* * *

Author Topic: Read TFilestream into a static array  (Read 543 times)

elcomportal

  • New member
  • *
  • Posts: 16
Read TFilestream into a static array
« 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

taazz

  • Hero Member
  • *****
  • Posts: 5336
Re: Read TFilestream into a static array
« Reply #1 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.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

elcomportal

  • New member
  • *
  • Posts: 16
Re: Read TFilestream into a static array
« Reply #2 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?

ASerge

  • Hero Member
  • *****
  • Posts: 900
Re: Read TFilestream into a static array
« Reply #3 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;

elcomportal

  • New member
  • *
  • Posts: 16
Re: Read TFilestream into a static array
« Reply #4 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.



Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 448
    • Lebeau Software
Re: Read TFilestream into a static array
« Reply #5 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, 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;
« Last Edit: July 11, 2018, 12:10:23 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus