TFileStream
From Lazarus wiki
Jump to navigationJump to search
│
Deutsch (de) │
English (en) │
français (fr) │
polski (pl) │
A TFileStream is a descendant of TStream that reads or writes data to a file on disk.
Constant | Decimal | Description |
---|---|---|
fmCreate | 65280 | Creates a new file or overwrites an existing file. |
fmOpenRead | 0 | Opens a file for reading only. |
fmOpenWrite | 1 | Opens a file for writing. |
fmOpenReadWrite | 2 | Opens a file for reading and writing (Modify Data). |
fmShareDenyWrite | 32 | Prevent other processes from writing to the file while it is open. |
fmShareDenyRead | 16 | Prevent other processes from reading the file while it is open. |
fmShareDenyNone | 64 | Allows other processes to read and write the file without restrictions. |
Read entire file fnam into a string
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;
Read 8 bytes of fnam into a byte array
..
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;
Write a string txt to the file fnam
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;