Sign in Join
Wiki copytemporary

binary file

Deutsch · English · Polski

Binary file

A binary file is suitable for processing files of any size. With binary files, the content of a file is read in character by character. Conversely, the file is written character by character. If a file is opened as a binary file, a character can be changed anywhere in the file. Data of the same type can now be written and read into a file defined with FILE OF as follows:

pascal
 var
   datFile : File of Byte;    // always reads and writes a character
   datFile : File of Char;    // always reads and writes a character
   datFile : File of Integer; // always reads and writes two characters
   ...

Any location in the binary file can be accessed with the SEEK command.

Create file

pascal
 var
   datFile    : File of Char;
   chrContent : Char;

 begin
   Assignfile(datFile, 'example.txt');  // Assigns the name of the file to the variable txtFile and opens the file
   ReWrite(datFile);                    // File will be overwritten if it exists
   chrContent := 'A';
   Write(datFile, chrContent);          // Write the first character to the new file 
   ...
 end;

Close file

pascal
 var
   datFile    : File of Char;
   chrContent : Char;

 begin
   ...
   CloseFile(datFile);  // closes the file
 end;

Add a character to the end of an existing file

For binary files, a character can be added to the end of a file using the Seek command.

pascal
 var
   datFile    : File of char;
   chrContent : Char;

 begin
   AssignFile(datFile, 'example.txt');
   Reset(datFile);                    // Sets the file pointer to the beginning of the file
   Seek(datFile, FileSize(datFile));  // Determines the end of the file and sets the file pointer to the end
   chrContent := 'b';
   Write(datFile, chrContent);        // Write another character at the end of the file
   ...

Replace a character in an existing file

For binary files, you can use the Seek command to replace a character anywhere in the file.

pascal
 var
   datFile    : File of Char;
   chrContent : Char;

 begin
   AssignFile(datFile, 'example.txt');
   Reset(datFile);                     // Sets the file pointer to the beginning of the file
   Seek (datFile, 1);                  // Set the file pointer to the _second_ character in the file
   chrContent := 'f';
   Write(datFile, chrContent);         // Overwrite the second character in the file
   ...

Read file completely

pascal
 var
   datFile    : File of Char;
   chrContent : Char;

 begin
   AssignFile(datFile, 'example.txt');
   Reset(datFile);
   
   while not eof(datFile)           // keep reading as long as there is data to read
     do begin
       read(datFile, chrContent);   // reads a single character into chrContent variable
       ...
     end;

   CloseFile(datFile);
 end;

See also


From the Free Pascal wiki, page "binary file", as it stood on 2022-01-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.