Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

TFileStream

Deutsch · English · Français · Polski

A TFileStream is a descendant of TStream that gets/stores its data from/to a file on disk.

Constant Decimal Description
fmCreate 65280 Creates a new file
fmOpenRead 0 opens a file for reading
fmOpenWrite 1 opens a file for writing
fmOpenReadWrite 2 opens a file for reading and writing
fmShareDenyWrite 32 prohibit writing if file is already opened
pascal
function readstream(fnam: string): string;
var
  strm: TFileStream;
  n: longint;
  txt: string;
begin
  txt := '';
  strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite);
  try
    n := strm.Size;
    SetLength(txt, n);
    strm.Read(txt[1], n);
  finally
    strm.Free;
  end;
  result := txt;
end;
pascal
..
type
    TByte8Array = array [0..7] of byte;
..
function readstream(fnam: string; position= integer): TByte8Array;
var
  strm : TFileStream;
begin
  strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite);
  try
    strm.Position:=position;
    strm.Read (Result[0],8);
  finally
    strm.Free;
  end;
end;
pascal
procedure writestream(fnam: string; txt: string);
var
  strm: TFileStream;
  n: longint;
begin
  strm := TFileStream.Create(fnam, fmCreate);
  n := Length(txt);
  try
    strm.Position := 0;
    strm.Write(txt[1], n);
  finally
    strm.Free;
  end;
end;

See also


From the Free Pascal wiki, page "TFileStream", as it stood on 2022-04-12 after a change by Bart, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .