Difference between revisions of "TFileStream"

From Free Pascal wiki
Jump to navigationJump to search
m
(Remove incorrect statement that TFileStream reads entire file. Avoid FreeAndNil of local variables.)
Line 1: Line 1:
 
{{TFileStream}}
 
{{TFileStream}}
  
A '''TFileStream''' is a descendant of [[TStream]] that gets/stores its data from/to a file on disk. A TFileStream reads entire file data into memory and vice versa.
+
A '''TFileStream''' is a descendant of [[TStream]] that gets/stores its data from/to a file on disk.
 
 
 
{| class="wikitable sortable"
 
{| class="wikitable sortable"
 
! Constant !! Decimal !! Description
 
! Constant !! Decimal !! Description
Line 27: Line 26:
 
|}
 
|}
  
read from file ''fnam''.
+
* read entire file ''fnam'' to a string.
 
<syntaxhighlight>
 
<syntaxhighlight>
function readstream( fnam: string ): string;
+
function readstream(fnam: string): string;
 
var
 
var
 
   strm: TFileStream;
 
   strm: TFileStream;
Line 36: Line 35:
 
begin
 
begin
 
   txt := '';
 
   txt := '';
   strm := TFileStream.Create( fnam, fmOpenRead or fmShareDenyWrite );
+
   strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite);
 
   try
 
   try
 
     n := strm.Size;
 
     n := strm.Size;
     SetLength( txt, n );
+
     SetLength(txt, n);
     strm.Read( txt[1], n );
+
     strm.Read(txt[1], n);
 
   finally
 
   finally
     FreeAndNil( strm );
+
     strm.Free;
 
   end;
 
   end;
 
   result := txt;
 
   result := txt;
Line 48: Line 47:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Write ''txt'' to file ''fnam''.
+
* Write a string ''txt'' to file ''fnam''.
 
<syntaxhighlight>
 
<syntaxhighlight>
procedure writestream( fnam: string; txt: string );
+
procedure writestream(fnam: string; txt: string);
 
var
 
var
 
   strm: TFileStream;
 
   strm: TFileStream;
 
   n: longint;
 
   n: longint;
 
begin
 
begin
   strm := TFileStream.Create( fnam, fmCreate );
+
   strm := TFileStream.Create(fnam, fmCreate);
   n := Length( txt );
+
   n := Length(txt);
 
   try
 
   try
 
     strm.Position := 0;
 
     strm.Position := 0;
     strm.Write( txt[1], n );
+
     strm.Write(txt[1], n);
 
   finally
 
   finally
     FreeAndNil( strm );
+
     strm.Free;
 
   end;
 
   end;
 
end;
 
end;

Revision as of 10:26, 25 April 2018

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 to 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;
  • Write a string txt to 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;

See also