10.3 Files

2026 Syllabus Objectives

By the end of this subtopic, you should be able to:

  • explain why files are needed
  • write pseudocode to handle text files that contain one or more lines

What is a file?

A file is a place where a computer program stores data so that the data can be used again later.

When a program is running, it can store values in variables. But variables usually only keep their values while the program is still running. When the program ends, that data is usually lost. A file solves this problem because it lets the program save data in a permanent storage location.

For example, if a program stores a list of fruit names, student marks, or usernames, it may need to keep that information after the program closes. Instead of losing the data, the program can save it in a file and use it again next time.

Why files are needed

Files are needed because programs often need to keep data for future use.

A file is useful when:

  • data must still be available after the program has finished
  • data needs to be read again later
  • large amounts of data need to be stored outside the program
  • information needs to be updated or added to over time

Imagine a program that stores the names of students in a class. If the names are only kept in variables, they disappear when the program closes. If the names are stored in a file, the program can open the file later and read the names again.

So, the main reason files are needed is permanent storage. This means the data stays saved and does not disappear when the program stops.

What is file handling?

File handling means using programming commands to work with files.

When a program handles a file, it may:

  • open the file
  • read data from the file
  • write data to the file
  • close the file

These are the basic actions used when working with text files.

What is a text file?

A text file stores data as lines of text. Each line can contain words, numbers, or symbols written as text.

In this syllabus, you need to handle text files that contain one or more lines. That means a file may have just one line, or it may have many lines. Your pseudocode must be able to deal with both cases.

For example, a text file might contain:

Apple
Banana
Orange
Grapes

Each fruit name is on a separate line. A program can read one line at a time until it reaches the end of the file.

Working with text files in pseudocode

Opening a file

Before a program can use a file, it must open it.

If the program wants to read data that is already stored in the file, it opens the file for read.

Example:

OPENFILE FruitFile FOR READ

This tells the program to open the file called FruitFile so that data can be read from it.

If the program wants to write new data into a file, it opens the file for write.

Example:

OPENFILE ShoppingFile FOR WRITE

This is used when the program wants to create a new file or replace the old contents with new contents.

If the program wants to add extra data to the end of an existing file, it opens the file for append.

Example:

OPENFILE ShoppingFile FOR APPEND

Appending means adding new data without deleting what is already there.

Reading from a text file

When a file is opened for reading, the program can read one line at a time.

Example:

READFILE FruitFile, FruitName

This means one line is read from FruitFile and stored in the variable FruitName.

If the file has many lines, the program usually uses a loop to keep reading until it reaches the end of the file.

End of file

A program needs a way to know when there are no more lines left to read. This is called end of file.

The function EOF() checks whether the end of the file has been reached.

Example:

WHILE NOT EOF(FruitFile) DO

This means the loop continues while the file has more lines to read.

This is very important because it stops the program from trying to read past the end of the file.

Closing a file

After the program has finished using a file, it should close it.

Example:

CLOSEFILE FruitFile

Closing the file is important because it shows the program is finished with it. It is good practice to always close a file after reading from it or writing to it.

A step-by-step example: counting lines in a file

Suppose a text file contains fruit names, one fruit per line. We want the program to:

  1. open the file for reading
  2. read each line
  3. count how many lines there are
  4. close the file
  5. output the total

Here is the pseudocode:

DECLARE FruitName : STRING
DECLARE FruitCount : INTEGER

FruitCount ← 0

OPENFILE FruitFile FOR READ

WHILE NOT EOF(FruitFile) DO
    READFILE FruitFile, FruitName
    FruitCount ← FruitCount + 1
ENDWHILE

CLOSEFILE FruitFile

OUTPUT "Total number of fruits: ", FruitCount

How this pseudocode works

First, the program declares two variables.

FruitName is a string because it stores the name of each fruit as text.

FruitCount is an integer because it stores a whole number.

The program starts by setting FruitCount to 0. This is because no lines have been read yet.

Next, the file is opened for reading.

The loop then begins. As long as the program has not reached the end of the file, it keeps reading.

Each time one line is read, the fruit name is stored in FruitName. Then the program adds 1 to FruitCount.

When there are no more lines left, the loop stops.

The file is then closed.

Finally, the program outputs the total number of fruit names that were in the file.

Why this example is important

This example shows the basic structure of text file handling in pseudocode:

  • declare variables
  • open the file
  • use a loop to read each line
  • process the line
  • close the file
  • output the result

This pattern can be used for many file-handling tasks.

Writing to a text file

A program may also need to save data into a file.

For example, if you want to store the word "Oranges" in a file, you can open the file for writing and then write the line.

Example:

OPENFILE FruitFile FOR WRITE
WRITE FruitFile, "Oranges"
CLOSEFILE FruitFile

This opens the file, writes the line, and closes the file.

If the file does not already exist, opening for write can create it. If the file already exists, writing may replace its old contents.

Appending to a text file

Sometimes you do not want to replace what is already in the file. Instead, you want to add extra lines at the end.

This is called appending.

Example:

OPENFILE FruitFile FOR APPEND
WRITE FruitFile, "Mango"
CLOSEFILE FruitFile

This adds "Mango" to the end of the file without removing the existing lines.

Handling files with one or more lines

The syllabus says text files may contain one or more lines. This means your pseudocode must not assume there are many lines. It should work even if there is only one line.

Using a loop with WHILE NOT EOF(...) DO is a good way to do this because:

  • if the file has one line, the loop runs once
  • if the file has many lines, the loop runs many times
  • the program stops at the correct place in both cases

This makes the pseudocode safe and flexible.

Things to remember when writing file-handling pseudocode

When answering exam questions, try to include the correct file-handling steps in the correct order.

A strong answer usually includes:

  1. Declare the variables you need.
  2. Open the file in the correct mode.
  3. Read or write the data.
  4. If reading many lines, use a loop with EOF().
  5. Close the file when finished.

If you miss out opening or closing the file, the pseudocode is incomplete.

Common file-handling commands in pseudocode

Here are some important commands you should know:

  • OPENFILE FileName FOR READ Opens a file so the program can read from it.

  • OPENFILE FileName FOR WRITE Opens a file so the program can write to it. It can create a new file or replace an old one.

  • OPENFILE FileName FOR APPEND Opens a file so the program can add new data at the end.

  • READFILE FileName, Variable Reads one line from the file and stores it in a variable.

  • WRITE FileName, Data Writes data to the file.

  • EOF(FileName) Checks whether the end of the file has been reached.

  • CLOSEFILE FileName Closes the file after use.

Example: reading all lines and outputting them

Here is another simple example. This program reads every line in a text file and displays it.

DECLARE Line : STRING

OPENFILE MyFile FOR READ

WHILE NOT EOF(MyFile) DO
    READFILE MyFile, Line
    OUTPUT Line
ENDWHILE

CLOSEFILE MyFile

This example is useful because it shows that the line read from the file does not have to be counted. It can also be displayed or used in another way.

Example: writing several lines to a file

Here is an example of writing lines to a file:

OPENFILE MyFile FOR WRITE
WRITE MyFile, "Ali"
WRITE MyFile, "Sara"
WRITE MyFile, "Hassan"
CLOSEFILE MyFile

This creates a file or replaces its contents, then writes three lines to it.

Example: adding one extra line to an existing file

OPENFILE MyFile FOR APPEND
WRITE MyFile, "Zara"
CLOSEFILE MyFile

This adds a new line to the end of the existing file.

Identifier table for the fruit-counting example

An identifier table helps explain what each name in a program means.

For the fruit example:

  • FruitNameSTRING — stores each fruit name read from the file
  • FruitCountINTEGER — stores the number of fruits read
  • FruitFileFILE — the file containing the fruit names

This helps make the pseudocode easier to understand.

Exam advice

In exam questions about files, students often lose marks by forgetting small but important steps.

Be careful to:

  • open the file in the correct mode
  • use READFILE correctly when reading
  • use a loop when there are multiple lines
  • check for end of file using EOF()
  • close the file at the end

Also, read the question carefully. If it says to read data, open the file for READ. If it says to store or save data, you may need WRITE or APPEND instead.

Key Terms

File — a storage location used by a program to save data so it can be used again later.

File handling — using programming commands to work with files, such as opening, reading, writing, and closing them.

Text file — a file that stores data as lines of text.

Read — to take data from a file into a program.

Write — to send data from a program into a file.

Append — to add new data to the end of a file without deleting what is already there.

End of file (EOF) — the point where there are no more lines left to read in a file.

Variable — a named storage place in a program that holds a value.

String — a data type used for text.

Integer — a data type used for whole numbers.

Identifier table — a table that shows the names used in a program, their data types, and what they are used for.

Exam-Style Questions & Answers

Question 1 (4 marks)

Explain why files are needed in computer programs.

Model Answer:

  1. Files are needed because data in variables is usually lost when the program stops running. A file allows the data to be kept for later use.
  2. Files allow a program to store information permanently, so it can be opened and used again in the future.
  3. Files are useful when a program needs to save large amounts of data outside the program itself. This makes the data easier to keep and reuse.
  4. Files are needed when data must be updated over time, such as lists of names or saved results, instead of being entered again every time.

Question 2 (4 marks)

A text file contains one fruit name on each line. Describe how a program can read all the lines in the file.

Model Answer:

  1. The program should first open the file using OPENFILE ... FOR READ so that it is ready to read data.
  2. It should use a loop to keep working while the end of the file has not been reached. This is usually done with WHILE NOT EOF(FileName) DO.
  3. Inside the loop, the program should read one line at a time using READFILE. Each line can be stored in a string variable.
  4. After all lines have been read, the program should close the file using CLOSEFILE to finish the file handling properly.

Question 3 (4 marks)

Write pseudocode to open a text file called NamesFile, read every line, and output each line.

Model Answer:

  1. A correct solution must declare a string variable to hold each line, for example DECLARE Name : STRING.
  2. The file must be opened for reading, for example OPENFILE NamesFile FOR READ.
  3. The program must use a loop and read each line, for example: WHILE NOT EOF(NamesFile) DO READFILE NamesFile, Name OUTPUT Name ENDWHILE
  4. The file must be closed at the end using CLOSEFILE NamesFile.

Question 4 (4 marks)

Explain the difference between opening a file for WRITE and opening a file for APPEND.

Model Answer:

  1. Opening a file for WRITE is used when the program wants to put data into the file as new output.
  2. WRITE can create a new file if it does not already exist, and it may replace the old contents of an existing file.
  3. Opening a file for APPEND is used when the program wants to add new data to the end of the file.
  4. APPEND keeps the existing data and adds extra lines after it, instead of replacing what is already stored.

Question 5 (4 marks)

A student writes pseudocode to count how many lines are in a text file. Give four steps that should appear in the pseudocode.

Model Answer:

  1. The pseudocode should include a variable for the count, such as an integer set to 0 before reading starts.
  2. It should open the file for reading, because the program needs to take data from the file.
  3. It should use a loop with NOT EOF(...) and read one line at a time, increasing the count after each line is read.
  4. It should close the file when the loop ends, and then output the final count

Sign in to view full notes