The File Handler
Open, read, write, close. Listing them is the easy part. Knowing what each one is for, and what quietly breaks when the last one is left off, is the part that earns marks.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
Four verbs, and one that gets forgotten
There are four things a program does with a file: open it, read from it, write to it, and close it. Listing them takes about five seconds and is worth roughly one mark. ⚠️ Three of those four get remembered. The fourth gets left off, and it is the one that causes actual trouble. So this module is much less about naming the operations than about what each one is FOR and what quietly goes wrong when it is missed. Start with why files exist at all. A variable holds its value while the program runs and loses it the moment the program stops. What you write to a file is still there afterwards - tomorrow, on a different day, after the machine has been switched off and on again. That difference is the entire point, and a question asking why a program uses a file rather than a variable is asking exactly that.
Words for working with files
Four operations and the property that makes them worth having.
Opening to read, opening to write
The open step asks two things: which file, and what you intend to do with it. The second one has consequences that the third column spells out.
How to answer on a sequence of steps
Some topics are a set of steps that have to happen in a particular order. This is one of them, and there is a way of answering them that consistently scores better than listing. Say what each step is FOR, in a few words. Not what it is called, which the question has usually told you already. ⚠️ Then say what breaks if it is skipped, or done out of order. That is where the marks concentrate, because it is the part that cannot be recited from a glossary. ⚠️ And pay particular attention to any step that looks like housekeeping - the tidy-up one, the one that feels optional, the one everybody leaves off. In an ordered sequence that step is very often the one being asked about, precisely because it separates people who have actually used the thing from people who have only read a list of it.
Match each step to what it is for
- Opening a file
- Reading from a file
- Writing to a file
- Closing a file
- saying which file is meant, and what kind of access the program needs to it
- bringing stored data off the disk and into the program to be used
- putting data from the program onto the disk so it outlives the program
- finishing properly, so nothing is left unsaved and nothing is left locked
The file that was never closed
A program writes several lines to a file and then finishes without ever closing it. What can go wrong?
- Some of what was written may never actually reach the disk, and the file can stay locked against other programs
- Nothing, because the program has ended, so the file must be finished with
- The file will be deleted automatically
- The data will be written to the file twice over
Opened the wrong way
A program is meant to add today's entry to a log file that already holds months of them. It opens the file for writing and puts the new line in. ⚠️ In many languages, opening a file for writing starts it again from empty. If that is what happened here, the months of entries were gone before the new line was ever added - and nothing about that looks like an error. No message, no crash. The program does exactly what it was told and reports success. A weak answer: "you should open it the right way." A strong answer: "Opening for writing declares that data is going in, and in many languages that means the file is started fresh. Adding to what is already there is a separate thing and has to be asked for specifically. So the risk is not that the program fails; it is that it succeeds while silently destroying what the file held." ⚠️ This is why the open step takes a choice as well as a filename. It looks like paperwork and it is the most consequential decision in the whole sequence. One thing this module deliberately does not cover: what the lines inside the file are made of, and how you pull them apart. Operations on the text itself belong to the string topic, and they are not part of file handling.
Order the file routine
Put the stages of a program working with a file into the order they happen.
- Open the file, saying which one it is and what access is needed
- Check that it actually opened, since the file may be missing or unavailable
- Read or write, as many times as the job requires
- Close the file, so everything is safely stored
- Carry on with the rest of the program, the file now free for anything else to use
Complete the file handling paragraph
Before a program can touch a file at all it must first _____ it, naming the file and stating the kind of access it needs. Bringing data from the disk into the program is called _____ from the file, and putting data from the program onto the disk is called _____ to it. Once the job is finished the program should _____ the file, so that everything is safely stored and nothing is left locked.
Name the step
Five situations, three lives. Say which of the four operations is being described.
Header lines and record lines
A program opens a file that holds 12 lines in total. It reads the first 3 lines as a header and treats every remaining line as one record. Work out how many record lines the file holds.
Spot the true file handling facts
Tap the TWO statements that are true.
- Data written to a file is still there after the program has ended
- A file has to be opened before it can be read from or written to
- Closing a file is optional, because ending the program does the same job
- Opening a file for writing always adds to the end of what is already there
Three file jobs
Three situations. Choose the answer that gives a reason, not just an operation.
- A program has to add today's entry to a log file that already holds months of entries. What must be got right at the open step?
- A program reads a settings file that will not exist yet on a brand new installation. What should it do?
- A classmate says that closing a file is just tidiness and makes no real difference. What do you tell them?
Explain the routine, and what skipping a step costs
A friend can list the four file operations but lost most of the marks on a question asking why each one matters. Write them the answer.
- Name the four operations a program performs on a file, in the order they happen
- Explain what the open step decides, beyond simply which file is meant
- Explain why data written to a file is different from data held in a variable
- Explain what can go wrong when a file is never closed
- Finish with the mistake at the open step that can destroy a file's contents, and how to avoid it