DelphiBasics
Seek
Procedure
Move the pointer in a binary file to a new record position System unit
 procedure Seek ( var FileHandle : File; RecordNumber : LongInt ) ;
Description
The Seek procedure moves the current record position in an open binary file given by FileHandle to a new position RecordNumber.
 
The file must have been assigned with AssignFile and opened with Reset or ReWrite.
 
For untyped files, the record size is set with the Reset or ReWrite routine used.
 
For typed files, the record size is SizeOf the file type.
 
The first record in a file is record 0.
Notes
Use SeekEoln or SeekEof to move the file pointer on a text file.
Related commands
Eof Returns true if a file opened with Reset is at the end
Eoln Returns true if the current text file is pointing at a line end
File Defines a typed or untyped file
FilePos Gives the file position in a binary or text file
SeekEoln Skip to the end of the current line or file
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Repositioning in a Word type file
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myWord, myWord1, myWord2, myWord3, myWord4 : 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 := 12;
  myWord2 := 34;
  myWord3 := 56;
  myWord4 := 78;
  Write(myFile, myWord1, myWord2, myWord3, myWord4);

  // Close the file
  CloseFile(myFile);

  // Reopen the file for read only purposes
  FileMode := fmOpenRead;
  Reset(myFile);

  // Seek (move) to the start of the 3rd record
  Seek(myFile, 2);    // Records start from 0

  // Show this record
  Read(myFile, myWord);
  ShowMessage('Record 3 = '+IntToStr(myWord));

  // Close the file
  CloseFile(myFile);
end;
 
end.
Hide full unit code
   Record 3 = 56
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page