Understanding basic file operations in Pascal will provide a clearer insight into a Pascal program and how to perform tasks such as creating, reading,... files from Pascal.
Pascal treats a file as a sequence of components, where file types must be consistent. The file type is defined by the component type. The file data type is defined as:
Data Type Definition
file-name = file of base-type;
Where base-type indicates the component type of the file, which can be integer, real number, Boolean, enumeration type, record type, array type, and set type excluding other file types. Variables of the file type are created using the declaration var.
Variable Declaration
f1, f2,...: file-name;
Below are some examples of defining various file types and file variables in Pascal:
Creating and Writing Files in Pascal
The following example demonstrates a program that can create a data file for the student record type. It will create a file named students.dat and write student data into it:
Upon compilation and execution of the above code, the program will create a file named students.dat in the active directory. You can open this file in text editors like Notepad.
Reading a File in Pascal
In the example above, you've just created and written to a file named students.dat. The next step now is to write a program that can read student data from the file:
Upon compilation and execution of the above code, it will return the following result:
File as Parameters in Subprograms
In Pascal, file variables can be used as parameters in both standard and user-defined subprograms. The following example will further illustrate this concept.
In the example below, the program creates a file named rainfall.txt which stores data about rainfall. It then opens the file, reads the data, and calculates the average rainfall.
Note that when using file parameters with subprograms, it must be declared as a var parameter.
When the above code is compiled and executed, it will return the following result:
Text File in Pascal
Text Files in Pascal contain lines of characters, with each line terminated by a period. The declaration and definition structure of a file is as follows:
type
file-name = text;
Regular character files and text files differ in that text files are divided into lines, each line ending with a period, automatically inserted by the system.
The following example creates and writes to a text file named contact.txt:
When the above code is compiled and executed, it will return the following result:
File Concatenation in Pascal
File concatenation in Pascal means appending some data to an existing file without overwriting the file. Below is an example program demonstrating file concatenation:
When the above code is compiled and executed, it will return the following result:
Basic File Handling Functions in Pascal
Free Pascal supports various functions and procedures for basic file operations in Pascal:
We hope that after reading this article by Mytour, readers will gain more useful information about Pascal and basic file operations in Pascal. Additionally, readers can refer to some other articles on Mytour to further understand operators in Pascal and arrays in Pascal.