RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Seek Function

Moves the current position of a file to a specified component.

Pascal
procedure Seek(var F; N: Longint);
C++
Seek( F, Longint N);

System

Use Seek in Delphi code to move to a specified position in open typed or untyped files. The current file position of F moves to component number N, where the number of the first component of a file is 0. 

In the above syntax, F is a typed or untyped file variable. The file specified by F must be open. N is an expression of type Longint 

To expand a file, seek one component beyond the last component; that is, the statement Seek(F, FileSize(F)) moves the current file position to the end of the file.

Note: {$I+} handles runtime errors using exceptions. When using {$I-}, use IOResult to check for I/O errors.
 

Delphi Examples: 

 

{
FileSize, Seek, FilePos example
This example displays a open dialog when the user clicks a button. When the user selects the file, it reports the size of the file, seeks to halfway through the file, and reports the file position at that point.
Note:   This example assumes the compiler flag $IOCHECKS ON ($I+). That is, it expects errors to generate exceptions rather than checking IOResult to be sure that I/O routines succeed.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
   f: file of Byte;
   size: Longint;
   S: string;
   y: Integer;
begin
  if OpenDialog1.Execute then
  begin
    AssignFile(f, OpenDialog1.FileName);
    Reset(f);
    try
      size := FileSize(f);
      S := 'File size in bytes: ' + IntToStr(size);
      y := 10;
      Canvas.TextOut(5, y, S);
      y := y + Canvas.TextHeight(S) + 5;
      S := 'Seeking halfway into file...';
      Canvas.TextOut(5, y, S);
      y := y + Canvas.TextHeight(S) + 5;
      Seek(f, size div 2);
      S := 'Position is now ' + IntToStr(FilePos(f));
      Canvas.TextOut(5, y, S);
    finally
      CloseFile(f);
    end;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!