Objectives: Students should be able to —
- 1 Explain why a program needs to store data in a file rather than in main memory (RAM).
- 2 Open a file for reading or for writing using the OPEN … FOR READ / WRITE statement.
- 3 Read a line of data from a file using READFILE and store it in a variable.
- 4 Write a line of data to a file using WRITEFILE.
- 5 Close a file using CLOSEFILE and explain why closing is essential.
- 6 Write a complete algorithm in pseudocode to read and display a line of text from a file.
- 7 Write pseudocode to input a line of text from the user and store it in a file.
- 8 Write pseudocode to copy a line of text from one file to another.
- 9 Describe sequential file access and use the EOF() function to read every record in a file.
- 10 Distinguish between overwriting and appending data to an existing file.
- 11 Write pseudocode that writes a set of records (multiple fields per record) to a file and reads them back.
- 12 Identify the four key file-handling keywords — OPEN, READFILE, WRITEFILE, CLOSEFILE — and trace their order in a typical program.
File Handling Basics: Why and How Programs Use Files
Data saved in a file is stored permanently and will not be lost even if the computer is switched OFF.
Data stored in a file can be accessed by the program at any time.
The same data file can be used by another program or by another computer.
OPEN is the keyword used to open a file, followed by the file's name (with its location or path) and its purpose.
FOR READ or FOR WRITE is used to mention its purpose.
Example: If the variable MyFile holds the name of the file, like —
MyFile <- "D:\Message.txt" Statement to open the file to read will be:
OPEN MyFile FOR READ Statement to open the file to write will be:
OPEN MyFile FOR WRITE READFILE is the keyword used to read data from a file, and WRITEFILE is the keyword used to write data into a file.
READFILE statement:
READFILE(MyFile), TextLine This statement reads data from the file named MyFile and stores it in the variable TextLine.
WRITEFILE statement:
WRITEFILE(MyFile), TextLine This statement writes the data held in the variable TextLine into the file named MyFile.
| Keyword | Purpose | Direction of data |
|---|---|---|
| READFILE | Read a line of data from a file | File → Variable |
| WRITEFILE | Write a line of data into a file | Variable → File |
CLOSEFILE is the keyword used to close a file, followed by the file's name (with its location or path) enclosed in parentheses ( ).
Closing the file releases the memory resources allocated by the operating system (OS) for that file. If the file is not closed, it will be left open until the system is closed.
Example: If the variable MyFile holds the name of the file —
MyFile <- "D:\Message.txt" Statement to close the file will be:
CLOSEFILE(MyFile) - Frees up the memory and OS file handles the program was using.
- Ensures any data still in the buffer is actually written (flushed) to the disk.
- Prevents data corruption that can occur if the program ends unexpectedly while the file is still open.
- Allows other programs to access the file (some operating systems lock an open file).
Pseudocode Programs: Reading, Writing and Copying Files
Step 1: Declare variables to hold the file's name and the text read from the file.
DECLARE MyFile, TextLine : STRING Step 2: Open the file for reading.
MyFile <- "Message.txt" OPEN MyFile FOR READ Step 3: Read and store the line of text from the file in the variable.
READFILE(MyFile), TextLine Step 4: Display the line of text.
OUTPUT "The file contains this line of text : ", TextLine Step 5: Close the file.
CLOSEFILE(MyFile) Step 1: Declare variables to hold the file's name and the text to be written into the file.
DECLARE FileToWrite, TextLine : STRING INPUT "Enter the line of text : ", TextLine Step 2: Open the file for writing.
FileToWrite <- "MyFile.txt" OPEN FileToWrite FOR WRITE Step 3: Write the line of text into the file, stored in the variable.
WRITEFILE(FileToWrite), TextLine Step 4: Close the file.
CLOSEFILE(FileToWrite) Step 1: Declare variables to hold the file names and the text to be copied.
DECLARE FileToRead, FileToWrite, TextLine : STRING Step 2: Open the source file for reading and read the line of text.
FileToRead <- "File1.txt" OPEN FileToRead FOR READ READFILE(FileToRead), TextLine Step 3: Open the destination file for writing and write the line of text.
FileToWrite <- "File2.txt" OPEN FileToWrite FOR WRITE WRITEFILE(FileToWrite), TextLine Step 4: Close both files.
CLOSEFILE(FileToRead) CLOSEFILE(FileToWrite) Sequential File Access & the EOF() Function
Sequential file access means reading (or writing) the records of a file one after another in order, from the beginning of the file to the end. You cannot jump directly to record number 50 — you must read records 1, 2, 3, … until you reach the one you want.
Text files in IGCSE pseudocode are always accessed sequentially.
The EOF() function (short for End Of File) returns TRUE when the program has reached the end of the file (i.e. there is no more data left to read), and FALSE while there is still data to read.
It is used as the condition in a WHILE loop so that the program keeps reading records until the end of the file is reached:
WHILE NOT EOF(MyFile) DO READFILE(MyFile), TextLine OUTPUT TextLine ENDWHILE The program uses a WHILE NOT EOF() loop so that lines are read and displayed one at a time until the end of the file is reached.
DECLARE MyFile, ThisLine : STRING MyFile <- "Names.txt" OPEN MyFile FOR READ WHILE NOT EOF(MyFile) DO READFILE(MyFile), ThisLine OUTPUT ThisLine ENDWHILE CLOSEFILE(MyFile) Trace:
- The file is opened FOR READ.
- Each iteration checks EOF(MyFile) — if not yet at the end, the next line is read into ThisLine and printed.
- When the last line has been read, the next call to EOF() returns TRUE; the loop terminates.
- The file is closed to release OS resources.
A FOR loop is used because the number of records (5) is known in advance. Each name is input and written to the file as a separate line.
DECLARE MyFile, ThisName : STRING DECLARE Counter : INTEGER MyFile <- "Students.txt" OPEN MyFile FOR WRITE FOR Counter <- 1 TO 5 OUTPUT "Enter student name: " INPUT ThisName WRITEFILE(MyFile), ThisName NEXT Counter CLOSEFILE(MyFile) Appending vs Overwriting Data in a File
| Mode | Pseudocode keyword | Effect on existing file contents |
|---|---|---|
| Overwrite | OPEN … FOR WRITE | A new empty file is created. Any previous contents of the file are lost. |
| Append | OPEN … FOR APPEND | The file is opened and the write pointer is positioned at the end. New data is added after the existing contents; original data is preserved. |
Example — overwrite (loses old data):
OPEN MyFile FOR WRITE // old contents erased WRITEFILE(MyFile), NewLine CLOSEFILE(MyFile) Example — append (keeps old data):
OPEN MyFile FOR APPEND // write pointer at end WRITEFILE(MyFile), NewLine CLOSEFILE(MyFile) To preserve the existing contents, the file must be opened FOR APPEND instead of FOR WRITE.
DECLARE MyFile, NewEntry : STRING INPUT "Enter the new log entry: ", NewEntry MyFile <- "Log.txt" OPEN MyFile FOR APPEND WRITEFILE(MyFile), NewEntry CLOSEFILE(MyFile) Result: NewEntry is added as a new line at the end of Log.txt. All previously stored log entries remain intact.
Records & Extended File Programs
(a) Read and display all records:
Each record has two fields, so the program must call READFILE twice per iteration of the loop — once for the Name and once for the Age.
DECLARE MyFile, ThisName : STRING DECLARE ThisAge : INTEGER MyFile <- "Students.txt" OPEN MyFile FOR READ WHILE NOT EOF(MyFile) DO READFILE(MyFile), ThisName READFILE(MyFile), ThisAge OUTPUT "Name: ", ThisName, " Age: ", ThisAge ENDWHILE CLOSEFILE(MyFile) (b) Number of READFILE calls per record:
Each record is made up of two fields (Name + Age), so READFILE is executed 2 times per record.
For a file containing N records, the loop runs N times and READFILE is called 2N times in total.
(a) Write 3 records to Students.txt:
DECLARE MyFile, ThisName : STRING DECLARE ThisAge, Counter : INTEGER MyFile <- "Students.txt" OPEN MyFile FOR WRITE FOR Counter <- 1 TO 3 OUTPUT "Enter student name: " INPUT ThisName OUTPUT "Enter age: " INPUT ThisAge WRITEFILE(MyFile), ThisName WRITEFILE(MyFile), ThisAge NEXT Counter CLOSEFILE(MyFile) (b) Read all records back and display names with Age ≥ 16:
OPEN MyFile FOR READ WHILE NOT EOF(MyFile) DO READFILE(MyFile), ThisName READFILE(MyFile), ThisAge IF ThisAge >= 16 THEN OUTPUT ThisName ENDIF ENDWHILE CLOSEFILE(MyFile) Note: The file is opened a second time (this time FOR READ) after it has been closed at the end of part (a). The IF statement inside the loop filters the records so only the names of students aged 16 or above are output.
Revision: Statements and Key File-Handling Terms
| Statement | Key Term |
|---|---|
| A named collection of data stored on backing storage (e.g. hard disk) that persists after the computer is switched off. | File |
| The temporary working storage (main memory) that holds variables and their values while a program runs. | RAM (main memory) |
| The pseudocode keyword used to make a file ready for reading or writing. | OPEN … FOR READ / WRITE |
| The pseudocode keyword that reads a line of data from a file into a variable. | READFILE |
| The pseudocode keyword that writes a line of data from a variable into a file. | WRITEFILE |
| The pseudocode keyword used to close a file and release the OS resources it was using. | CLOSEFILE |
| A function that returns TRUE when the program has reached the end of a file. | EOF() |
| Reading or writing the records of a file one after another in order, from start to end. | Sequential file access |
| A single complete set of related fields stored in a file (e.g. one student's Name + Age). | Record |
| An individual data item within a record, such as a Name or an Age. | Field |
| Opening a file FOR WRITE so that any previous contents are erased and a new empty file is started. | Overwriting |
| Opening a file FOR APPEND so that new data is added after the existing contents, which are preserved. | Appending |
| A small area of main memory used to temporarily hold data being transferred between a program and a file. | Buffer |
| The unique location of a file on a storage device, e.g. D:\Message.txt. | File path |
| A run-time error produced when a program tries to read past the end of a file without checking EOF(). | EOF / "read past end of file" error |
| A text file whose extension indicates it stores human-readable characters organised into lines. | .txt (text file) |