Hi,and that works? Have you opened the file with a hex editor to conferm it has the data or the size it suppose to have? In any case in order to read an arbitrary number of bytes from a file you use the the Read method of the fs variable. just make sure when you create the Tfilestream you do not use fmcreate as a value it will delete the old file and create a new with zero size. use fmOpenReadwrite or fmshareexclusive eg
with the following code i write the content of an static array into a file:Now i want to read it back from the file into the array, but i searched and nothing found how to do this. Please help me...
var fs:TFileStream; stream: array[1..8,1..32000000] of byte; i:integer; ... for i:=1 to 8 do begin fs:=TFileStream.create('d://stream_cpu.dat',fmCreate); try fs.write(stream[i],sizeOf(stream[i])); finally fs.free; end; end;
Thank you.
Torsten
with the following code i write the content of an static array into a file:You write only the last element, because each time you create a new file, i.e. erase the old one.
... for i:=1 to 8 do begin fs:=TFileStream.create('d://stream_cpu.dat',fmCreate); try fs.write(stream[i],sizeOf(stream[i])); finally fs.free; end; end;
It will make 8 files.
var
fs: TFileStream;
stream: array[1..8, 1..32000000] of byte;
i: integer;
...
for i := 1 to 8 do
begin
fs := TFileStream.Create('d:\mdf\projects\stream_cpu' + IntToStr(i) + '.dat', fmCreate);
try
fs.WriteBuffer(stream[i], SizeOf(stream[i]));
finally
fs.free;
end;
end;
var
fs: TFileStream;
stream: array[1..8, 1..32000000] of byte;
i: integer;
...
for i := 1 to 8 do
begin
fs := TFileStream.Create('d:\mdf\projects\stream_cpu' + IntToStr(i) + '.dat', fmOpenRead or fmShareDenyWrite);
try
fs.ReadBuffer(stream[i], SizeOf(stream[i]));
finally
fs.free;
end;
end;
const
NumFiles = 8;
MaxBytes = 32000000;
var
fs: TFileStream;
stream: array[1..NumFiles, 1..MaxBytes] of byte;
i: integer;
...
for i := 1 to NumFiles do
begin
fs := TFileStream.Create('d:\mdf\projects\stream_cpu' + IntToStr(i) + '.dat', fmCreate);
try
fs.WriteBuffer(stream[i], MaxBytes);
finally
fs.free;
end;
end;
const
NumFiles = 8;
MaxBytes = 32000000;
var
fs: TFileStream;
stream: array[1..NumFiles, 1..MaxBytes] of byte;
i: integer;
...
for i := 1 to NumFiles do
begin
fs := TFileStream.Create('d:\mdf\projects\stream_cpu' + IntToStr(i) + '.dat', fmOpenRead or fmShareDenyWrite);
try
fs.ReadBuffer(stream[i], MaxBytes);
finally
fs.free;
end;
end;