Variables Delphi

var FileMode : Integer;


Description
The FileMode variable defines the mode in which the Reset procedure opens a typed or untyped binary file.

The Reset procedure opens a non-text file for read, write, or read+write access as defined by FileMode :

fmOpenRead = 0 = Read only
fmOpenWrite = 1 = Write only
fmOpenReadWrite = 2 = Read and write (default)

Use Seek to move the current file position. This is useful for selective updating or for appending to the end of the file.

Notes
Warning : the default is Read and write. Opening a read-only file (such as on a CD Rom) would therefore fail.


Related commands
AssignFile Assigns a file handle to a binary or text file
Reset Open a text file for reading, or binary file for read/write

Example code : Demonstrate all 3 file access modes
var
myWord, myWord1, myWord2, myWord3 : Word;
myFile : File of Word;
begin
// Try to open the Test.cus binary file in write only mode
AssignFile(myFile, 'Test.cus');
FileMode := fmOpenWrite;
ReSet(myFile);
// Write a few lines of Word data to the file
myWord1 := 123;
myWord2 := 456;
myWord3 := 789;
Write(myFile, myWord1, myWord2, myWord3);
// Close the file
CloseFile(myFile);
// Reopen the file in read only mode
FileMode := fmOpenRead;
Reset(myFile);
// Display the file contents
ShowMessage('File contents at the start');
while not Eof(myFile) do
begin
Read(myFile, myWord);
ShowMessage(IntToStr(myWord));
end;
// Close the file again
CloseFile(myFile);
// Reopen the file in read+write mode
FileMode := fmOpenReadWrite;
Reset(myFile);
// Read one Word of data, then overwrite the next
Read(myFile, myWord);
myWord := 9876;
Write(myFile, myWord);
// Close the file
CloseFile(myFile);
// Reopen the file in read only mode
FileMode := fmOpenRead;
Reset(myFile);
// Display the file contents
ShowMessage('Updated file contents');
while not Eof(myFile) do
begin
Read(myFile, myWord);
ShowMessage(IntToStr(myWord));
end;
// Close the file for the last time
CloseFile(myFile);
end;

Show full unit code
File contents at the start
123
456
789
Updated file contents
123
9876
789