AS Level · Topic 10.3
A-Level Computer Science 9618 / Topic 10 / AS

Data that stays.

Variables and arrays live in main memory and vanish when the program stops. A text file on secondary storage keeps the data for next time. Learn why files are needed and the exact 9618 pseudocode to read, write and append lines of text.

The key idea

Data in variables is held in RAM and is lost when the program ends. A file is stored on secondary storage, so the data persists and can be used the next time the program runs or by another program. A text file is a sequence of lines, each read or written as a string, in order. A file is opened in one mode: READ, WRITE (creates a new file; any existing data is lost) or APPEND (adds after the existing data). Use READFILE, WRITEFILE, the EOF function and CLOSEFILE.

a text file outlives the program that wrote it Program (RAM) variables and arrays lost when it ends Names.txt AshaBenCarlos one string per line WRITEFILE READFILE modes: READ, WRITE (new file, old data lost), APPEND (add to the end) files are stored on secondary storage, so the data persists
Fig. 1: lines are written to and read from a text file that remains after the program ends
Section 01

Why files?

Any data that must outlive a single run of a program has to be saved somewhere other than main memory.

StatementWhat it does
OPENFILE <file> FOR READopen for reading, from the first line
OPENFILE <file> FOR WRITEcreate a new file; any existing data in it is lost
OPENFILE <file> FOR APPENDadd new lines after the existing data
READFILE <file>, <variable>read the next line into a STRING variable
WRITEFILE <file>, <data>write the data as a new line
EOF(<file>)TRUE if there are no more lines to read
CLOSEFILE <file>close the file when it is no longer needed
Examiner trap

"Files are needed to store data" is too vague. Say that variables are lost when the program ends, so the data must be stored permanently in a file to be used again later.

Section 02

Reading a file.

In the lab, run the program that writes three names to a file and then reads one back. Then imagine you do not know how many lines there are: that is what EOF is for.

DECLARE LineOfText : STRING
DECLARE Total : INTEGER
DECLARE CountA : INTEGER
Total ← 0
CountA ← 0
OPENFILE "Names.txt" FOR READ
WHILE NOT EOF("Names.txt")
    READFILE "Names.txt", LineOfText
    Total ← Total + 1
    IF MID(LineOfText, 1, 1) = "A" THEN
        CountA ← CountA + 1
    ENDIF
ENDWHILE
CLOSEFILE "Names.txt"
OUTPUT CountA, " of ", Total, " names start with A"
Worked example Trace the read loop
Names.txt contains five lines: Asha, Ben, Carlos, Abdul, Bea. State the output.
  1. Line 1 Asha: Total 1, first character A, CountA 1.
  2. Line 2 Ben: Total 2. Line 3 Carlos: Total 3.
  3. Line 4 Abdul: Total 4, CountA 2. Line 5 Bea: Total 5.
  4. No lines are left, so EOF returns TRUE and the loop ends.
Answer: 2 of 5 names start with A
Your turn
Why is a WHILE loop used here rather than a REPEAT ... UNTIL EOF("Names.txt") loop?
Examiner trap

READFILE reads one line into a STRING variable. If a line holds a number, it is still text until it is converted, so do not declare the READFILE variable as INTEGER.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Why are files needed in a program?

Quick check+10 XP

When does EOF("Names.txt") return TRUE?

Quick check+10 XP

Which file mode adds new lines after the existing contents?

Quick check+10 XP

In the lab, three names are written to Names.txt, the file is closed and reopened FOR READ, then one READFILE is done. Which name is read?

Section 03

Writing and appending.

WRITE starts a brand-new file every time it is opened; APPEND keeps what is there and adds to the end. A file is open in only one mode at a time, so close it and open it again to switch.

DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "Clean.txt" FOR WRITE
WHILE NOT EOF("FileA.txt")
    READFILE "FileA.txt", LineOfText
    IF LineOfText <> "" THEN
        WRITEFILE "Clean.txt", LineOfText
    ENDIF
ENDWHILE
CLOSEFILE "FileA.txt"
CLOSEFILE "Clean.txt"

OPENFILE "Log.txt" FOR APPEND
WRITEFILE "Log.txt", "Clean.txt created"
CLOSEFILE "Log.txt"
Worked example Copy without blank lines
FileA.txt holds five lines: Red, (blank), Blue, (blank), Green. Clean.txt already exists and holds one line, Old. What does Clean.txt hold after the program runs?
  1. Opening Clean.txt FOR WRITE creates a new file, so the line Old is lost.
  2. Red is not empty, so it is written. The blank line is skipped.
  3. Blue is written, the next blank line is skipped, then Green is written.
  4. Log.txt was opened FOR APPEND, so its earlier lines are kept and one line is added at the end.
Answer: Clean.txt holds three lines: Red, Blue, Green
Your turn
A program records a new temperature in Temps.txt every hour, but the file only ever holds one reading. Suggest the cause and the fix.
Examiner trap

Always CLOSEFILE when you have finished. An examiner checks that every OPENFILE has a matching CLOSEFILE, with the same file name, after the loop rather than inside it.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Scores.txt already holds 20 lines. A program runs OPENFILE "Scores.txt" FOR WRITE. What happens to the 20 lines?

Exam style+20 XP

Which is the correct structure for reading every line of Data.txt?

Exam style+20 XP

What data type should the variable in READFILE "Data.txt", LineIn be?

Exam style+20 XP

Days.txt holds the lines Mon, Tue, Wed, Thu. A program opens it FOR READ, executes READFILE "Days.txt", Day twice, then outputs Day. What is output?

Skill unlocked

Text files, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Why files are needed, the READ, WRITE and APPEND modes, and pseudocode to read, write, copy and append lines of a text file.