DelphiBasics
FilePos
Function
Gives the file position in a binary or text file System unit
1 function FilePos(var FileHandle File;):LongInt;
2 function FilePos (var FileHandle TextFile;;
Description
The FilePos function returns the current position in an open file. The returned value is the Record position, starting at 0 for the file beginning.
 
The file may be an Untyped, Typed or Text file.
 
The record length is as follows:
 
Untyped files  : As set in Reset or ReWrite
Typed files  : SizeOf the file type
Text files  : Variable - defined by line ends
Notes
Seek to the given record position only works for binary files.
Related commands
EolnReturns true if the current text file is pointing at a line end
FileDefines a typed or untyped file
SeekMove the pointer in a binary file to a new record position
SeekEolnSkip to the end of the current line or file
TextFileDeclares a file type for storing lines of text
 Download this web site as a Windows program.




 
Example code : Show the file position in a binary file
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');
  ReWrite(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
  while not Eof(myFile) do
  begin
    Read(myFile, myWord);
    // Note - FilePos shows the after read position
    ShowMessage('Record '+
                IntToStr(FilePos(myFile))+' = '+
                IntToStr(myWord));
  end;

  // Close the file for the last time
  CloseFile(myFile);
end;
Show full unit code
  Record 1 = 123
  Record 2 = 456
  Record 3 = 789
 
Delphi Programming © Neil Moffatt 2002 - 2022. All rights reserved.  |  Home Page