Link to home
Start Free TrialLog in
Avatar of libbysharf
libbysharf

asked on

TFileStream

How can I use TFileStream.create instead of the tradition assignFile and reset or rewrite for typed and untyped files?
Can I read the stream into a simple record/string and write from the record/string?
And finally, where can I found some examples of how to do it?

Thanks very much,
Libby


Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

var
  F: TFileStream;

F := TFileStream.Create('filename.abc', fmOpenRead)

is similar to

AssignFile
Reset

F := TFileStream.Create('filename.abc', fmOpenWrite)

is similar to using Rewrite

and F := TFileStream.Create('filename.abc', fmOpenReadWrite or fmCreate)

will create the file if it does not exists, but if it does, will open it similar to Reset

let's say you have a record

type
  TMyRec = record
    A, B, C: Integer;
  end;

var
  MyRec: TMyRec;

to read from stream to record:

F.ReadBuffer(MyRec, SizeOf(TMyRec));

and to write

F.WriteBuffer(MyRec, SizeOf(TMyRec));

F.Size is similar to FileSize, and F.Position tells you where the record pointer is.

more examples... check the help file?
to read/wrte as string you can cast it to TStringStream
ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial