Name

FilePos Function

Syntax

function FilePos(var F: File): LongInt;
function FilePos(var F: TextFile): LongInt;

Description

FilePos returns the current position (as a record number) in the file F. The beginning of the file is position zero. If F is a TextFile, the record size is arbitrarily chosen as the buffer size, which defaults to 128. When Eof(F) is True, FilePos returns the number of records in the file. FilePos is not a real function.

Errors

  • If the file F is not open, FilePos reports I/O error 103.

  • Although you can get the file position in a text file, you cannot use it to seek to that position. The Seek procedure works only with binary files. To get a file position of a text file, use the Windows API:

// Return a byte position in a text file if its buffer is empty.
function TextFilePos(var F: TextFile): LongInt;
begin
  Result := SetFilePointer(TTextRec(F).Handle, 0, nil, File_Current);
end;
  • FilePos does not support files larger than 2 GB. See the FileSeek function in the SysUtils unit, or call the Windows API for large files.

See Also

Eof Function, File Keyword, FileSize Function, IOResult Function, Seek Procedure, TextFile Type, Truncate Procedure

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.