44 total
By the end of this subtopic, you should be able to:
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.
Files are needed because programs often need to keep data for future use.
A file is useful when:
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.
File handling means using programming commands to work with files.
When a program handles a file, it may:
These are the basic actions used when working with text files.
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.
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.
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.
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.
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.
Suppose a text file contains fruit names, one fruit per line. We want the program to:
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
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.
This example shows the basic structure of text file handling in pseudocode:
This pattern can be used for many file-handling tasks.
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.
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.
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:
This makes the pseudocode safe and flexible.
When answering exam questions, try to include the correct file-handling steps in the correct order.
A strong answer usually includes:
EOF().If you miss out opening or closing the file, the pseudocode is incomplete.
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.
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.
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.
OPENFILE MyFile FOR APPEND
WRITE MyFile, "Zara"
CLOSEFILE MyFile
This adds a new line to the end of the existing file.
An identifier table helps explain what each name in a program means.
For the fruit example:
STRING — stores each fruit name read from the fileINTEGER — stores the number of fruits readFILE — the file containing the fruit namesThis helps make the pseudocode easier to understand.
In exam questions about files, students often lose marks by forgetting small but important steps.
Be careful to:
READFILE correctly when readingEOF()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.
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.
Explain why files are needed in computer programs.
Model Answer:
A text file contains one fruit name on each line. Describe how a program can read all the lines in the file.
Model Answer:
OPENFILE ... FOR READ so that it is ready to read data.WHILE NOT EOF(FileName) DO.READFILE. Each line can be stored in a string variable.CLOSEFILE to finish the file handling properly.Write pseudocode to open a text file called NamesFile, read every line, and output each line.
Model Answer:
DECLARE Name : STRING.OPENFILE NamesFile FOR READ.WHILE NOT EOF(NamesFile) DO
READFILE NamesFile, Name
OUTPUT Name
ENDWHILECLOSEFILE NamesFile.Explain the difference between opening a file for WRITE and opening a file for APPEND.
Model Answer:
WRITE is used when the program wants to put data into the file as new output.WRITE can create a new file if it does not already exist, and it may replace the old contents of an existing file.APPEND is used when the program wants to add new data to the end of the file.APPEND keeps the existing data and adds extra lines after it, instead of replacing what is already stored.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:
NOT EOF(...) and read one line at a time, increasing the count after each line is read.Sign in to view full notes