HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Arrays and Files in Pseudocode
  1. 1One-dimensional arrays
  2. 2Two-dimensional arrays and nested loops
  3. 3Reading and writing files
  1. Computer Science
  2. /Arrays and Files in Pseudocode
  3. /Reading and writing files

Course 8.3·Lesson 3 of 3

Reading and writing files

Variables and arrays are lost when a program stops. This lesson explains why programs store data in files, and how Cambridge pseudocode opens, reads, writes and closes a text file, beside the same steps in Python.

about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Arrays and Files in Pseudocode
  1. 1One-dimensional arrays
  2. 2Two-dimensional arrays and nested loops
  3. 3Reading and writing files

Try the code and the simulations beside the lesson.

Why store data in a file?

A game shows the high score when you start it again tomorrow. Where was that score while the computer was off? Not in a variable. Variables and arrays live in main memory, and they are lost when the program stops. A file is kept on storage. So a program stores data in a file when it must be kept after the program ends, or used later by this program or by another one. A file can also bring in a large amount of data that nobody wants to type again.

The Python Going Further book on this site has a whole chapter, "Files and Data Formats". The syllabus needs only the simple part: text files, read and written one item or one line at a time.

The four file commands

  • OPENFILE <name> FOR READ opens a file so that data can be read from it.
  • OPENFILE <name> FOR WRITE opens a file for writing. A new file is created, and any data already in that file is lost.
  • READFILE <name>, <variable> reads the next item, a line of the file, into the variable.
  • WRITEFILE <name>, <value> writes a value into the file, as a new line.
  • CLOSEFILE <name> closes the file when it is no longer needed.

Open a file in one mode at a time. Always close it. The file name is a string, such as "Scores.txt".

Writing and reading back

This program saves a high score:

pseudocode
DECLARE PlayerName : STRING
DECLARE HighScore : INTEGER
PlayerName ← "Amira"
HighScore ← 87
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", PlayerName
WRITEFILE "Scores.txt", HighScore
CLOSEFILE "Scores.txt"

The file Scores.txt now holds two lines: Amira and 87. Later, a second program loads them:

pseudocode
DECLARE BestName : STRING
DECLARE BestScore : INTEGER
OPENFILE "Scores.txt" FOR READ
READFILE "Scores.txt", BestName
READFILE "Scores.txt", BestScore
CLOSEFILE "Scores.txt"
OUTPUT BestName, " has the high score of ", BestScore

The items are read in the order they were written. The output is Amira has the high score of 87.

Two boxes. Left, main memory, lost when the program stops, holding the variables PlayerName "Amira" and HighScore 87. Right, storage, the file Scores.txt, kept after the program stops, holding line 1: Amira and line 2: 87. An arrow labelled WRITEFILE goes from memory to the file, and an arrow labelled READFILE goes back.

The picture shows the journey. WRITEFILE copies values from variables in memory into the file on storage. READFILE copies them back into variables, line by line.

Copying a line, and reading into an array

The syllabus example copies one line of text from one file to another:

pseudocode
DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "FileB.txt" FOR WRITE
READFILE "FileA.txt", LineOfText
WRITEFILE "FileB.txt", LineOfText
CLOSEFILE "FileA.txt"
CLOSEFILE "FileB.txt"

To read several values, put READFILE in a loop. The program must know how many lines there are, because the syllabus has no command to test for the end of a file. Here five temperatures are read into an array:

pseudocode
DECLARE Temps : ARRAY[1:5] OF INTEGER
DECLARE Index : INTEGER
OPENFILE "Temps.txt" FOR READ
FOR Index ← 1 TO 5
    READFILE "Temps.txt", Temps[Index]
NEXT Index
CLOSEFILE "Temps.txt"

If Temps.txt holds 18, 21, 19, 24 and 22, the array holds them in the same order. Temps[4] is 24.

Practise with new numbers

Try it in Python

The program writes a file, closes it and reads it back, then shows what happens when a file is opened for writing a second time. Python's "w" is WRITE and "r" is READ.

Read it first. Then run it. Which name is left in the file at the end?

1. Change the second "w" to "a", which appends in Python. What is in the file now? The pseudocode of the syllabus has no append mode.

Common mistakes

  • Reading from a file opened FOR WRITE. Open it FOR READ first.
  • Opening FOR WRITE by mistake. It wipes the file.
  • Forgetting CLOSEFILE. Close every file you open.
  • Reading in a different order from the order the data was written.
  • Reading past the last line. Know how many items the file holds.

Remember these

  • Files keep data after the program stops; variables do not.
  • OPENFILE ... FOR READ or FOR WRITE, then READFILE or WRITEFILE, then CLOSEFILE.
  • WRITE mode creates a new file and loses the old data.
  • READFILE reads the next line into a variable; WRITEFILE writes a value as a line.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Give one reason for storing a list of names in a file rather than in an array.
  1. Write the statement that opens Marks.txt so that the program can read it.
  1. A file holds three lines. A program opens it FOR WRITE, writes one line and closes it. How many lines does it hold now?
  1. In the temperature program, what is Temps[2]?
  1. Why must the high-score program read the name before the score?

Enroll free in Arrays and Files in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…Easy · High School (9–10) · by StemPy Lab
Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…Medium · High School (9–10) · by StemPy Lab
A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course
← Arrays and Files in Pseudocode
← Two-dimensional arrays and nested loopsNext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Python Labs (1)

Saving and loading a listPython Lab · by StemPy Lab

Question Sets (1)

Arrays and Files: Test Yourself10 questions · by StemPy Lab