← Computer Science

File Handling

Variables vanish the moment a program stops, so when we need data to survive we write it to a file on backing storage. In this lesson we cover why files are used and the Cambridge pseudocode text-file operations: opening a file for READ, WRITE or APPEND, reading to the end of the file, writing and closing it.

Slide 1 / 4

Why programs use files

RAM is lost on power off; a file persistsVariables in RAMdatapower offempty (lost)Data in a filedatapower offdata kept
  • Variables are stored in RAM, which is volatile, so their contents are lost when the program ends or the power is turned off.
  • A file is stored on backing storage such as a disk, so the data persists and is still there the next time the program runs.
  • This lets a program save results, settings or records and load them back later instead of asking the user to type everything again.
  • At this level we work with text files, which store data as readable characters, usually one record or line at a time.

Check you've got it

0/4

1. What is the main reason a program stores data in a file rather than only in variables?

2. A file already contains data. What happens if it is opened using OPENFILE "scores.txt" FOR WRITE and then written to?

3. During a reading loop, what does EOF("data.txt") return TRUE for?

4. A program must add one new record to a file without deleting the records already stored. Which mode should be used?

Practise real exam questions on this

Type your answer, see your mark against the mark scheme in seconds. Free, 5 a day.

Start practising →

Teaching this topic?

Mark your whole class's answers and see who's stuck, for £19 a month.

See the class plan →

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.