Write pseudocode to handle text files that consist of one or more lines

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Topic 10.3 Files

Topic 10.3 – Files

Learning Objective

Write pseudocode to handle text files that consist of one or more lines.

Key Concepts

  • File modes – read, write, append.
  • Opening and closing a file.
  • Reading a line, reading all lines, writing a line.
  • End‑of‑File (EOF) detection.
  • Typical file‑handling patterns in pseudocode.

File‑Handling Operations

OperationPseudocode SyntaxDescription
Open file for readingOPEN fileName FOR READ AS fileCreates a file handle for sequential reading.
Open file for writingOPEN fileName FOR WRITE AS fileCreates a new file (or overwrites existing) for sequential writing.
Open file for appendingOPEN fileName FOR APPEND AS fileOpens an existing file and positions the pointer at the end.
Read a lineREADLINE file INTO lineReads the next line of text; returns EOF when no more lines.
Write a lineWRITELINE file, lineWrites the string line followed by a newline character.
Close fileCLOSE fileReleases the file handle and flushes buffers.

Pseudocode Patterns

Reading an entire file line‑by‑line

  1. Open the file for reading.
  2. Loop until EOF is reached.
  3. Process each line as required.
  4. Close the file.

Example:

OPEN "data.txt" FOR READ AS f

WHILE NOT EOF(f) DO

  READLINE f INTO currentLine

  // Process currentLine, e.g., count words

END WHILE

CLOSE f

Writing multiple lines to a new file

  1. Open the file for writing.
  2. For each line to be written, use WRITELINE.
  3. Close the file.

Example:

OPEN "output.txt" FOR WRITE AS f

FOR i FROM 1 TO n DO

  line ← "Record " & i

  WRITELINE f, line

END FOR

CLOSE f

Appending a line to an existing file

This pattern is useful for log files.

OPEN "log.txt" FOR APPEND AS f

WRITELINE f, "Program started at " & CURRENTTIME()

CLOSE f

Handling Files with an Unknown Number of Lines

When the number of lines is not known in advance, the EOF test controls the loop.

The total number of lines read can be stored in a variable lineCount:

lineCount ← 0

OPEN "input.txt" FOR READ AS f

WHILE NOT EOF(f) DO

  READLINE f INTO line

  lineCount ← lineCount + 1

  // Additional processing here

END WHILE

CLOSE f

/* lineCount now holds the number of lines in the file */

Mathematically, if \$L\$ is the number of lines, then after the loop \$lineCount = L\$.

Common Errors and How to Avoid Them

  • Forgetting to close a file – can lead to data loss or file‑locking issues.
  • Reading past EOF – always test EOF before calling READLINE.
  • Mixing read and write modes on the same handle – open separate handles or reopen the file in the required mode.
  • Incorrect newline handling – remember that WRITELINE adds a newline automatically.

Suggested Diagram

Suggested diagram: Flowchart showing the sequence of opening a file, looping with EOF test, processing each line, and closing the file.

Practice Exercise

  1. Write pseudocode that reads a text file containing integer values (one per line) and calculates their sum and average.
  2. Modify the above pseudocode to also count how many of the integers are even.
  3. Write pseudocode to create a new file that lists each original integer followed by its square, one per line.

Answer Sketch for Exercise 1

sum ← 0

count ← 0

OPEN "numbers.txt" FOR READ AS f

WHILE NOT EOF(f) DO

  READLINE f INTO line

  value ← STRINGTOINT(line)

  sum ← sum + value

  count ← count + 1

END WHILE

CLOSE f

IF count > 0 THEN

  average ← sum / count

ELSE

  average ← 0

END IF

/* sum and average now contain the results */