AS · Practice questions · Text files

Open, read, close.

Six original Cambridge-style questions on text file handling in 9618 pseudocode, with trace tables and mark points.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

One mode at a time.

01
Explain
[2 marks]

Explain why a program that keeps a list of high scores needs to store them in a file.

  • Variables and arrays are held in RAM and are lost when the program ends. ✓
  • A file is held on secondary storage, so the scores are still available the next time the program runs. ✓
02
Recall
[3 marks]

Describe the effect of opening a text file in each of these modes: READ, WRITE, APPEND.

  • READ: data can be read from the file, starting at the first line. ✓
  • WRITE: a new file is created for writing; any existing data in the file is lost. ✓
  • APPEND: data is written after the existing data in the file. ✓
03
Trace
[4 marks]

Stock.txt contains five lines: Pen, Ink, (blank line), Pad, Pin. State the output of this pseudocode.

Count ← 0
OPENFILE "Stock.txt" FOR READ
WHILE NOT EOF("Stock.txt")
    READFILE "Stock.txt", Item
    IF Item <> "" THEN
        IF MID(Item, 1, 1) = "P" THEN
            OUTPUT Item
        ENDIF
    ELSE
        Count ← Count + 1
    ENDIF
ENDWHILE
CLOSEFILE "Stock.txt"
OUTPUT Count
  • Pen is output (starts with P). ✓
  • Ink is not output; the blank line adds 1 to Count. ✓
  • Pad and Pin are output. ✓
  • Final output 1 (the number of blank lines). ✓
04
Write
[5 marks]

Names : ARRAY[1:30] OF STRING is a global array; unused elements hold the empty string. Write pseudocode for a procedure SaveNames() that writes every non-empty name to a new file Names.txt, one per line.

  • PROCEDURE SaveNames() ... ENDPROCEDURE. ✓
  • OPENFILE "Names.txt" FOR WRITE before the loop. ✓
  • FOR Index ← 1 TO 30 ... NEXT Index. ✓
  • IF Names[Index] <> "" THEN WRITEFILE "Names.txt", Names[Index]. ✓
  • CLOSEFILE "Names.txt" after the loop. ✓
05
Write
[6 marks]

Words.txt contains an unknown number of lines. Write pseudocode to read the lines into Words : ARRAY[1:100] OF STRING, stopping at the end of the file or when the array is full, and then output how many lines were stored.

  • Declare a counter Count ← 0 and a STRING variable for the line. ✓
  • OPENFILE "Words.txt" FOR READ. ✓
  • WHILE NOT EOF("Words.txt") AND Count < 100. ✓
  • READFILE "Words.txt", LineIn, then Count ← Count + 1. ✓
  • Words[Count] ← LineIn inside the loop, after the increment. ✓
  • CLOSEFILE "Words.txt" and OUTPUT Count after the loop. ✓
06
Analysis
[3 marks]

A program adds one line to Visitors.txt each time a visitor signs in. It uses OPENFILE "Visitors.txt" FOR WRITE. At the end of the day the file holds only one name. Explain why and describe how to correct the program.

  • Opening in WRITE mode creates a new file each time. ✓
  • So every earlier name is lost and only the last visitor remains. ✓
  • Open the file FOR APPEND, so each name is added after the existing lines (and close it after writing). ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.