Difference between revisions of "TFileStream"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "{{TFileStream}} A '''TFileStream''' is a descendant of TStream that get its data from a file on disk. A TFileStream reads entire file data into memory. {| class="wikit...")
 
Line 34: Line 34:
 
var
 
var
 
   strm: TFileStream;
 
   strm: TFileStream;
 +
  txt: string;
 
begin
 
begin
 +
  txt := '';
 
   strm := TFileStream.Create( fnam, fmOpenRead or fmShareDenyWrite );
 
   strm := TFileStream.Create( fnam, fmOpenRead or fmShareDenyWrite );
 
   try
 
   try
     SetLength( readstream, strm.Size );
+
     SetLength( txt, strm.Size );
     dat.Read( readstream[1], strm.Size );
+
     dat.Read( txt[1], strm.Size );
 
   finally
 
   finally
 
     FreeAndNil( strm );
 
     FreeAndNil( strm );
 
   end;
 
   end;
 +
  result := txt;
 
end;
 
end;
  

Revision as of 00:52, 26 August 2016

Deutsch (de) English (en) français (fr) polski (pl)

A TFileStream is a descendant of TStream that get its data from a file on disk. A TFileStream reads entire file data into memory.


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


function readstream( fnam: string ): string;
var
  strm: TFileStream;
  txt: string;
begin
  txt := '';
  strm := TFileStream.Create( fnam, fmOpenRead or fmShareDenyWrite );
  try
    SetLength( txt, strm.Size );
    dat.Read( txt[1], strm.Size );
  finally
    FreeAndNil( strm );
  end;
  result := txt;
end;


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
    FreeAndNil( strm );
  end;
end;