File Handling in Pascal

July 25, 2024

File Handling in Pascal

File handling is an essential aspect of programming that allows us to store and retrieve data from external files. In this post, we will explore how to work with files in Pascal, including opening, closing, reading, and writing data to files.

Types of Files in Pascal

In Pascal, files can be categorized into two main types:

  • Text Files: These files contain human-readable characters and can be edited with any text editor. They are typically used for logging or storing simple data.
  • Binary Files: These files contain data in binary format and are not human-readable. They are used for storing complex data structures or large amounts of data efficiently.

Declaring File Variables

Before using a file, you need to declare a file variable. The syntax for declaring a file variable in Pascal is as follows:

var
    myFile: TextFile;  { For text files }
    myBinaryFile: File; { For binary files }

Opening a File

To work with a file, you must first open it. The AssignFile procedure is used to associate a file variable with a specific file, and the Reset and Rewrite procedures are used to open existing files for reading and create new files for writing, respectively.

AssignFile(myFile, 'data.txt');  { Associate myFile with data.txt }
Reset(myFile);  { Open the file for reading }
AssignFile(myFile, 'newdata.txt');  { Associate myFile with newdata.txt }
Rewrite(myFile);  { Create a new file for writing }

Reading from a File

To read data from a file, you can use the ReadLn procedure for text files. This procedure reads a line from the file and stores it in a variable.

var
    line: string;
begin
    AssignFile(myFile, 'data.txt');
    Reset(myFile);
    while not Eof(myFile) do  { Loop until the end of the file }
    begin
        ReadLn(myFile, line);  { Read a line from the file }
        WriteLn(line);  { Output the line to the console }
    end;
    CloseFile(myFile);  { Close the file after reading }
end;

Writing to a File

To write data to a file, you can use the WriteLn procedure for text files. This procedure writes a line to the file.

begin
    AssignFile(myFile, 'output.txt');
    Rewrite(myFile);
    WriteLn(myFile, 'Hello, Pascal!');  { Write a line to the file }
    WriteLn(myFile, 'This is a test.');
    CloseFile(myFile);  { Close the file after writing }
end;

Closing a File

It is important to close a file after you are done reading or writing to it. You can use the CloseFile procedure to do this:

CloseFile(myFile);  { Close the file }

Binary File Handling

Binary files require a slightly different approach. You can use the BlockRead and BlockWrite procedures to read and write data in blocks.

var
    data: array[1..100] of Integer;
    count: Integer;
begin
    AssignFile(myBinaryFile, 'data.bin');
    Rewrite(myBinaryFile);
    for count := 1 to 100 do
        data[count] := count * 2;  { Populate the array with data }
    BlockWrite(myBinaryFile, data, SizeOf(data));  { Write the array to the binary file }
    CloseFile(myBinaryFile);
end;

Conclusion

File handling in Pascal is a straightforward process that allows you to read from and write to files effectively. By mastering these file operations, you can manage data persistence in your applications. In this post, we covered the basics of file handling, including declaring file variables, opening files, reading and writing data, and closing files. Now it’s your turn to practice these concepts and implement file handling in your Pascal programs!