Wednesday, 2 February 2011

Pascal Lesson 11 - Data Files

Learn Pascal Programming Tutorial Lesson 11 - Data Files

Data files are different from text files in a few ways. Data files are random access which means you don't have to read through them line after line but instead access any part of the file at any time. Here is how you declare a data file:
program DataFiles;

var
   f: file of Byte;

begin
end.

We then use Assign in the same way as we do with a text file.
program DataFiles;

var
   f: file of Byte;

begin
   Assign(f,'MyFile.dat');
end.

You can use Rewrite to create a new file or overwrite an existing one. The difference between text files and data files when using Rewrite is that data files can be read and written to.
program DataFiles;

var
   f: file of Byte;

begin
   Assign(f,'MyFile.dat');
   Rewrite(f);
end.

Reset is the same as Rewrite except that it doesn't overwrite the file.
program DataFiles;

var
   f: file of Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
end.

When you write to a file using the Write command you must first put the value to be written to the file into a variable. Before you can write to or read from a data file you must use the Seek command to find the right place to start writing. You must also remember that data files start from position 0 and not 1.
program DataFiles;

var
   f: file of Byte;
   b: Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
   b := 1;
   Seek(f,0);
   Write(f,b);
end.

The Read command is used to read from a data file.
program DataFiles;

var
   f: file of Byte;
   b: Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
   Seek(f,0);
   Read(f,b);
end.

You must close a data file when you are finished with it just like with text files.
program DataFiles;

var
   f: file of Byte;
   b: Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
   Seek(f,0);
   Read(f,b);
   Close(f);
end.

The FileSize command can be used with the FilePos command to find out when you have reached the end of the file. FileSize returns the actual number of records which means it starts at 1 and not 0. The FilePos command will tell at what position in the file you are.
program DataFiles;

var
   f: file of Byte;
   b: Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
   while FilePos(f) <> FileSize(f) do
      begin
         Read(f,b);
         Writeln(b);
      end;
   Close(f);
end.

The Truncate command will delete everything in the file from the current position.
program DataFiles;

var
   f: file of Byte;

begin
   Assign(f,'MyFile.dat');
   Reset(f);
   Seek(f,3);
   Truncate(f);
   Close(f);
end.

One of the most useful things about data files is that you can use them to store records.
program DataFiles;

type
   StudentRecord = Record
      Number: Integer;
      Name: String;
   end;

var
   Student: StudentRecord;
   f: file of StudentRecord;

begin
   Assign(f,'MyFile.dat');
   Rewrite(f);
   Student.Number := 12345;
   Student.Name := 'John Smith';
   Write(f,Student);
   Close(f);
end.

No comments:

Post a Comment