FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Pascal Language FILE

file


     The File keyword is used to define a variable as a binary file, normally as written to and read from a storage device.

     Syntax 1: var MyFile:File;

     Gives a base untyped file. Such a file can only be read from and written to using BlockRead and BlockWrite. The basic data type is a byte.

     Assign must be used to get a file handle.

     Reset or ReWrite are then used to open the file for read and/or write access. Their second parameter specifies the number of bytes that comprise one 'record' as seen by the BlockRead and BlockWrite routines.

     Syntax 2: vat MyFile:File of Some Type;

     Defines the file with a base data type. For example, a simple type such as a char or a complex type such as a Packed Record.

     Assign must be used to get a file handle.

     Reset or ReWrite are then used to open the file for read and/or write access. Calling Read and/or Write must be used to access the file.

     In all cases, the type must be of fixed size, and access to the file must be in units of that type.

Example

    Download Source IconDownload This Source for Free Pascal
var
  myFile    : File;
  byteArray : array[1..8] of byte;
  oneByte   : byte;
  i : Integer;
  myWord, myWord1, myWord2 : Word;
  myFileW : File of Word;

begin
  // Try to open the Test.byt file for writing to
  Assign(myFile, 'File.001');
  Rewrite(myFile, 4);   // Define a single 'record' as 4 bytes

  // Fill out the data array
  for i := 1 to 8 do byteArray[i] := i;

  // Write the data array to the file
  BlockWrite(myFile, byteArray, 2);   // Write 2 'records' of 4 bytes

  // Close the file
  Close(myFile);

  // Reopen the file for reading
  Reset(myFile, 1);   // Now we define one record as 1 byte

  // Display the file contents
  while not Eof(myFile) do begin
    BlockRead(myFile, oneByte, 1);   // Read and display one byte at a time
    Writeln(oneByte);
  end;

  // Close the file for the last time
  Close(myFile);

  // Try to open the Test.bin binary file for writing to
  Assign(myFileW, 'File.002');
  Rewrite(myFileW);

  // Write a couple of lines of Word data to the file
  myWord1 := 234;
  myWord2 := 567;
  Write(myFileW, myWord1, myWord2);

  // Close the file
  Close(myFileW);

  // Reopen the file in read only mode
  Reset(myFileW);

  // Display the file contents
  while not Eof(myFileW) do begin
    Read(myFileW, myWord);
    Writeln(myWord);
  end;

  // Close the file for the last time
  Close(myFileW);
end.

Output

1
2
3
4
5
6
7
8
234
567

See Also

Append, Assign, BlockRead, BlockWrite, Close, Reset, Rewrite, Text.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


I am very happy to see that FoFPC is alive, we need more developers working on this great language.
Matias Vara
toro.sourceforge.net
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #2 in 0.003302 secs.

sponsor
Click to visit our paid sponsor