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

Handling Text Files Using Pseudocode (Cambridge A-Level Computer Science)

Text files are used to store data permanently. A text file consists of one or more lines of characters. Programs must be able to open, read, write, and close these files correctly.

Cambridge A-Level Computer Science uses specific pseudocode file handling commands which must be followed exactly in exams.


Key File Handling Commands

Command Purpose
OPENFILE filename FOR READ Open file for reading
OPENFILE filename FOR WRITE Open file for writing (overwrites file)
OPENFILE filename FOR APPEND Open file to add data to end
READFILE filename, variable Read one line from file
WRITEFILE filename, data Write data to file
CLOSEFILE filename Close file after use
EOF(filename) Checks if end of file reached


Reading a Text File Line by Line

When a file has multiple lines, a loop must be used to read each line until the end of the file.

Example text file Names.txt:

Ali

Mary

John

Pseudocode:

OPENFILE "Names.txt" FOR READ

WHILE NOT EOF("Names.txt")

READFILE "Names.txt", Name

OUTPUT Name

ENDWHILE

CLOSEFILE "Names.txt"

Explanation:

  • The file is opened for reading
  • The loop continues until the end of file
  • Each line is read into a variable
  • The file is closed after reading


Writing Multiple Lines to a Text File

Example pseudocode:

OPENFILE "Names.txt" FOR WRITE

WRITEFILE "Names.txt", "Ali"

WRITEFILE "Names.txt", "Mary"

WRITEFILE "Names.txt", "John"

CLOSEFILE "Names.txt"

Each WRITEFILE statement creates a new line in the file.


Appending Data to an Existing File

APPEND adds new data without deleting existing data.

OPENFILE "Names.txt" FOR APPEND

WRITEFILE "Names.txt", "David"

CLOSEFILE "Names.txt"


Reading and Counting Lines in a File

Example:

OPENFILE "Names.txt" FOR READ

Count ← 0

WHILE NOT EOF("Names.txt")

READFILE "Names.txt", Name

Count ← Count + 1

ENDWHILE

CLOSEFILE "Names.txt"

OUTPUT Count

This counts how many lines are in the file.


Searching for Data in a File

Example:

OPENFILE "Names.txt" FOR READ

Found ← FALSE

INPUT SearchName

WHILE NOT EOF("Names.txt")

READFILE "Names.txt", Name

IF Name = SearchName THEN

Found ← TRUE

ENDIF

ENDWHILE

CLOSEFILE "Names.txt"

IF Found = TRUE THEN

OUTPUT "Name found"

ELSE

OUTPUT "Name not found"

ENDIF


Writing User Input to a File

OPENFILE "Names.txt" FOR APPEND

INPUT Name

WRITEFILE "Names.txt", Name

CLOSEFILE "Names.txt"


Important Exam Rules

  • Always OPENFILE before using file
  • Always CLOSEFILE after use
  • Use WHILE NOT EOF to read multiple lines
  • Each READFILE reads one line
  • Each WRITEFILE writes one line


Common Exam Mistakes

Mistake:

READFILE "Names.txt", Name

READFILE "Names.txt", Name

READFILE "Names.txt", Name

Correct method:

WHILE NOT EOF("Names.txt")

READFILE "Names.txt", Name

ENDWHILE


Summary

  • Text files store data in lines
  • OPENFILE is used to open files
  • READFILE reads one line
  • WRITEFILE writes one line
  • EOF detects end of file
  • WHILE loop used to read multiple lines
  • CLOSEFILE must always be used


Exam-Style Questions

1. Write pseudocode to read and display all lines from a text file.


2. Write pseudocode to count number of lines in a file.


3. Write pseudocode to add a new line to a file.


4. Write pseudocode to search for a name in a file.


5. Explain why CLOSEFILE must be used.