Lazarus

Programming => General => Topic started by: elcomportal on July 10, 2018, 09:19:45 pm

Title: Read TFilestream into a static array
Post by: elcomportal on July 10, 2018, 09:19:45 pm
Hi,
with the following code i write the content of an static array into a file:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs:TFileStream;
  3.   stream: array[1..8,1..32000000] of byte;
  4.   i:integer;
  5. ...
  6.   for i:=1 to 8 do
  7.   begin
  8.       fs:=TFileStream.create('d://stream_cpu.dat',fmCreate);
  9.       try
  10.            fs.write(stream[i],sizeOf(stream[i]));
  11.       finally
  12.            fs.free;
  13.       end;
  14.   end;
  15.  
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...
Thank you.
Torsten
Title: Re: Read TFilestream into a static array
Post by: taazz on July 10, 2018, 09:31:40 pm
Hi,
with the following code i write the content of an static array into a file:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs:TFileStream;
  3.   stream: array[1..8,1..32000000] of byte;
  4.   i:integer;
  5. ...
  6.   for i:=1 to 8 do
  7.   begin
  8.       fs:=TFileStream.create('d://stream_cpu.dat',fmCreate);
  9.       try
  10.            fs.write(stream[i],sizeOf(stream[i]));
  11.       finally
  12.            fs.free;
  13.       end;
  14.   end;
  15.  
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...
Thank you.
Torsten
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
Code: Pascal  [Select][+][-]
  1.       fs:=TFileStream.create('d://stream_cpu.dat',fmOpenReadWrite or FmShareExclusive);
  2.  
Title: Re: Read TFilestream into a static array
Post by: elcomportal on July 10, 2018, 09:49:47 pm
Yes, it works. I've checked the files.
But how can i read it back into the array?
Title: Re: Read TFilestream into a static array
Post by: ASerge on July 10, 2018, 10:57:56 pm
with the following code i write the content of an static array into a file:
Code: Pascal  [Select][+][-]
  1. ...
  2.   for i:=1 to 8 do
  3.   begin
  4.       fs:=TFileStream.create('d://stream_cpu.dat',fmCreate);
  5.       try
  6.            fs.write(stream[i],sizeOf(stream[i]));
  7.       finally
  8.            fs.free;
  9.       end;
  10.   end;
You write only the last element, because each time you create a new file, i.e. erase the old one.
Easier then so. Write:
Code: Pascal  [Select][+][-]
  1. fs := TFileStream.Create('d:\stream_cpu.dat', fmCreate);
  2. try
  3.   fs.WriteBuffer(stream, SizeOf(stream));
  4. finally
  5.   fs.Free;
  6. end;
Read:
Code: Pascal  [Select][+][-]
  1. fs := TFileStream.Create('d:\stream_cpu.dat', fmOpenRead);
  2. try
  3.   fs.ReadBuffer(stream, SizeOf(Stream));
  4. finally
  5.   fs.Free;
  6. end;
Title: Re: Read TFilestream into a static array
Post by: elcomportal on July 10, 2018, 11:11:50 pm
original it's
Code: Pascal  [Select][+][-]
  1. fs:=TFileStream.create('d://mdf/projects/stream_cpu'+inttostr(i)+'.dat',fmCreate);
It will make 8 files. I've cropped this line for be better readable here in the forum.


Title: Re: Read TFilestream into a static array
Post by: Remy Lebeau on July 11, 2018, 12:06:04 am
It will make 8 files.

To write the files:

Code: [Select]
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;

To read the files:

Code: [Select]
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;

I would use constants instead of magic numbers (https://stackoverflow.com/questions/47882/), though:

Code: [Select]
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;

Code: [Select]
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;
TinyPortal © 2005-2018