Difference between revisions of "TFileStream"
From Lazarus wiki
Jump to navigationJump to searchm (Fixed syntax highlighting; typos) |
|||
Line 46: | Line 46: | ||
result := txt; | result := txt; | ||
end; | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | * read 8 bytes of ''fnam'' into a byte array: | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | function readstream(fnam: string; position= integer): string; | ||
+ | var | ||
+ | Buffer:array [0..7] of byte; | ||
+ | strm : TFileStream; | ||
+ | begin | ||
+ | strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite); | ||
+ | try | ||
+ | strm:=TFileStream.Create (fnam,fmShareDenyNone); | ||
+ | strm.Position:=position; | ||
+ | strm.Read (Buffer[0],8); | ||
+ | finally | ||
+ | strm.Free; | ||
+ | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 07:56, 12 April 2022
│
Deutsch (de) │
English (en) │
français (fr) │
polski (pl) │
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 |
- 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:
function readstream(fnam: string; position= integer): string;
var
Buffer:array [0..7] of byte;
strm : TFileStream;
begin
strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite);
try
strm:=TFileStream.Create (fnam,fmShareDenyNone);
strm.Position:=position;
strm.Read (Buffer[0],8);
finally
strm.Free;
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;