File Opening Mode for TFileStream in Delphi


In Delphi, the TFileStream can be used to stream the file reading and writing. Below is a sample usage that appends a ‘A’ to a file everytime.

program Test;
{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes;

var
  fs: TFileStream;
  v: byte;

begin
  fs := TFileStream.Create('C:\test.txt', fmOpenWrite);
  v := 65;
  try
    fs.Seek(0, soEnd);
    fs.WriteBuffer(v, SizeOf(v));
  finally
    fs.Free;
  end;
end.

However, since the file is opened only in write mode (fmOpenWrite), if you try to add fs.ReadBuffer(v, SizeOf(v)); you will have an exception ‘Stream Read Error‘ raised.

readerror File Opening Mode for TFileStream in Delphi beginner code code library delphi implementation programming languages technical

The File Open modes are defined in SysUtils.pas as below.

{$IFDEF MSWINDOWS}
  fmOpenRead       = $0000;
  fmOpenWrite      = $0001;
  fmOpenReadWrite  = $0002;

  fmShareCompat    = $0000 platform; // DOS compatibility mode is not portable
  fmShareExclusive = $0010;
  fmShareDenyWrite = $0020;
  fmShareDenyRead  = $0030 platform; // write-only not supported on all platforms
  fmShareDenyNone  = $0040;
{$ENDIF}

We can see that the fmOpenReadWrite is not as the same as fmOpenRead or fmOpenWrite.

The fmShareExclusive specifies that the file cannot be read or write by other processes.

The fmShareDenyWrite specifies that the file can by read but not write by other processes.

The fmShareDenyRead specifies that the file can by write but not read by other processes.

The fmShareDenyNone specifies that the file can by read or write (no limitation) by other processes.

If you specify the fmCreate the file will always be cleared as empty if created or always exists. In this case, if you specify fmCreate or fmOpenWrite, it is actually the same as fmCreate or fmOpenReadWrite, in such case, you can use ReadBuffer to read the data you have written before (or zero if it has not been written by WriteBuffer).

How about the time difference if you use fmOpenReadWrite or fmOpenWrite whichever makes no difference?

program Test;
{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils,
  Classes;

var
  fs: TFileStream;
  i: integer;
  v: byte;
  start: LongWord;

begin
  start := GetTickCount;
  fs := TFileStream.Create('C:\test.txt', fmOpenWrite or fmCreate);
  v := 65;
  try
    fs.Seek(0, soBeginning);
    for i := 0 to 10000000 do
    begin
      fs.WriteBuffer(v, SizeOf(v));
    end;
  finally
    fs.Free;
  end;
  Writeln(IntToStr(GetTickCount - start));
  start := GetTickCount;
  fs := TFileStream.Create('C:\test.txt', fmOpenReadWrite or fmCreate);
  v := 65;
  try
    fs.Seek(0, soBeginning);
    for i := 0 to 10000000 do
    begin
      fs.WriteBuffer(v, SizeOf(v));
    end;
  finally
    fs.Free;
  end;
  Writeln(IntToStr(GetTickCount - start));
  Readln;
end.

The timing is roughly the same (but using fmOpenWrite if you intend no read might be a slightly bit faster).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
518 words
Last Post: Linux Shell Programming - yes
Next Post: Quick Improvements on your TFileStream Access in Delphi

The Permanent URL is: File Opening Mode for TFileStream in Delphi

Leave a Reply