Sign in Join
FPDoc Onlineexperiment

FileSeek function

In unit sysutils

Set the current file position on a file handle.

Declaration

pascal
function FileSeek(Handle: THandle; FOffset: LongInt; Origin: LongInt)
: LongInt

Description

FileSeek sets the file pointer on position Offset, starting from Origin. Origin can be one of the following values:

fsFromBeginning
Offset is relative to the first byte of the file. This position is zero-based. i.e. the first byte is at offset 0.
fsFromCurrent
Offset is relative to the current position.
fsFromEnd
Offset is relative to the end of the file. This means that Offset can only be zero or negative in this case.

If successful, the function returns the new file position, relative to the beginning of the file.

The above mentioned constants do not exist in Delphi.

Errors

On error, -1 is returned.

See also

Example: ex37

pascal
Program Example37;

{ This program demonstrates the FileCreate/FileSeek/FileReed/FileTruncate functions }

Uses sysutils;

Var I,J,F : Longint;

Begin
  F:=FileCreate ('test.dat');
  If F=-1 then
    Halt(1);
  For I:=0 to 100 do
    FileWrite(F,I,SizeOf(i));
  FileClose(f);
  F:=FileOpen ('test.dat',fmOpenRead);
  For I:=0 to 100 do
    begin
    FileRead (F,J,SizeOF(J));
    If J<>I then
      Writeln ('Mismatch at file position ',I)
    end;
  FileSeek(F,0,fsFromBeginning);
  Randomize;
  Repeat
    FileSeek(F,Random(100)*4,fsFromBeginning);
    FileRead (F,J,SizeOf(J));
    Writeln ('Random read : ',j);
  Until J>80;
  FileClose(F);
  F:=FileOpen('test.dat',fmOpenWrite);
  I:=50*SizeOf(Longint);
  If FileTruncate(F,I) then
    Writeln('SuccessFully truncated file to ',I,' bytes.');
  FileClose(F);
End.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.