File Handling (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Purpose of files

What is file handling?

  • File handling is the use of programming techniques to work with information stored in text files

  • Examples of file handing techniques are:

    • opening text files

    • reading text files

    • writing text files

    • closing text files

Concept

CIE A Level Pseudocode

Open file for reading

OPENFILE "fruit.txt" FOR READ

Open file for writing

OPENFILE "Shopping.txt" FOR WRITE

Close file

CLOSEFILE "fruit.txt"

Read line

READFILE "fruit.txt", FruitName

Write line

WRITEFILE "fruit.txt", "Oranges"

Check end of file

IF NOT EOF("fruit.txt") THEN

Create new file

OPENFILE "Shopping.txt" FOR WRITE (same as writing – it overwrites or creates)

Append to file

OPENFILE "Shopping.txt" FOR APPEND

  • An example program written in pseudocode to:

    • Opens a text file for reading

    • Reads each line (a fruit name)

    • Counts how many fruits are in the file

    • Outputs the total

    • Closes the file

DECLARE FruitFile : STRING DECLARE FruitName : STRING DECLARE FruitCount : INTEGER

FruitFile ← "fruit.txt" FruitCount ← 0

OPENFILE FruitFile FOR READ

WHILE NOT EOF(FruitFile) 
  READFILE FruitFile, FruitName 
  FruitCount ← FruitCount + 1 
ENDWHILE

CLOSEFILE FruitFile

OUTPUT "Total number of fruits: ", FruitCount

Identifier table

Identifier

Data type

Description

FruitName

STRING

Stores the name of each fruit read from the file

FruitCount

INTEGER

Keeps track of how many fruits have been read

FruitFile

STRING

Holds the name of the file being read, "fruit.txt"

Worked Example

A program is required to read the contents of a text file Data.txt into a 1D array DataArray. The file contains 25 integer values, one per line.

Write pseudocode to:

  • open the file for reading

  • read each value into the array

  • close the file

[4]

Answer

  • Correct use of OPENFILE "Data.txt" FOR READ[1 mark]

  • Loop using WHILE NOT EOF("Data.txt") DO … ENDWHILE or FOR COUNTER ← 1 TO 25NEXT Counter [1 mark]

  • Correct use of READFILE "Data.txt", LineData storing into array with index DataArray[Counter] ← LineData[1 mark]

  • Correct use of CLOSEFILE "Data.txt" in an appropriate place … [1 mark]

Model answer:

DECLARE DataArray : ARRAY[1:25] OF STRING DECLARE Counter : INTEGER DECLARE LineData : STRING

Counter ← 1

OPENFILE "Data.txt" FOR READ

WHILE NOT EOF("Data.txt") 
  READFILE "Data.txt", LineData DataArray[Counter] ← LineData Counter ← Counter + 1 
ENDWHILE

CLOSEFILE "Data.txt"

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Build on this topic

Robert Hampton

Author: Robert Hampton

Expertise: Curriculum Expert

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Portfolio Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.