1. Overview
File handling is how programs store and retrieve data on a computer's storage devices (like hard drives or USB drives). This allows programs to remember information even after they are closed, making them more useful and capable. Without file handling, programs would only be able to work with data temporarily held in memory.
Key Definitions
- File: A named collection of data stored on a storage device.
- Persistent Storage: Storage that retains data even when the power is turned off. Files provide persistent storage.
- Text File: A file containing data encoded as human-readable characters (e.g., .txt, .csv).
- Binary File: A file containing data encoded in a non-human-readable format (e.g., .exe, .jpg).
- Open (a file): To prepare a file for reading or writing.
- Read (from a file): To retrieve data from a file and store it in a variable.
- Write (to a file): To store data from a variable into a file.
- Append (to a file): To add new data to the end of an existing file.
- Close (a file): To release the file and its resources after reading or writing.
- EOF (End Of File): A marker indicating the end of a file.
- Record: A collection of related data items treated as a unit. (Often, but not always, stored on a single line).
Core Content
Why use files?
- Permanent Storage: Data stored in files remains even after the program is closed.
- Saving Program State: Store settings, user preferences, or game progress.
- Storing User Data: Store user profiles, documents, or media files.
- Sharing Data: Share data between different programs. For example, a spreadsheet program can read data created by a database program.
- Backup Data: Create copies of important data in case of data loss.
- Handling Large Datasets: Work with data that is too large to fit entirely in memory.
File Types
| Feature | Text Files | Binary Files |
|---|---|---|
| Readability | Human-readable | Not human-readable |
| Storage | Stores characters, numbers, text | Stores any type of data (images, programs) |
| Size | Generally larger (due to character encoding) | Generally smaller |
| Examples | .txt, .csv, .html | .exe, .jpg, .mp3 |
File Operations
- Opening a File:
OPENFILE filename FOR READ- Opens an existing file for reading data.OPENFILE filename FOR WRITE- Creates a new file for writing data. If the file already exists, it will be overwritten.OPENFILE filename FOR APPEND- Opens an existing file to add data to the end. If the file does not exist, it creates a new file.
- Reading from a File:
READFILE filename, variable- Reads a single item of data (e.g., a number, a word) from the file and stores it in thevariable.READLINE filename, variable- Reads an entire line of text from the file and stores it in thevariable. This includes any spaces and punctuation in that line until a newline character.
- Writing to a File:
WRITEFILE filename, data- Writes a single item ofdata(e.g., a number, a word) to the file.WRITELINE filename, data- Writesdatato the file and adds a newline character at the end, creating a new line.
- Closing a File:
CLOSEFILE filename- Closes the file, releasing the resources used. Crucially important: always close the file after use.
- Checking for End of File:
EOF(filename)- ReturnsTRUEif the end of the file has been reached,FALSEotherwise. Used in loops to read all data from a file.
Pseudocode Examples
- Writing to a file:
OPENFILE "names.txt" FOR WRITE // Create a new file called names.txt
WRITEFILE "names.txt", "Alice" // Write "Alice" to the file
WRITELINE "names.txt", "Bob" // Write "Bob" to the file and add a newline
CLOSEFILE "names.txt" // Close the file
- Reading from a file:
OPENFILE "names.txt" FOR READ // Open the file names.txt for reading
WHILE NOT EOF("names.txt")
READLINE "names.txt", name // Read a line from the file and store it in the variable 'name'
OUTPUT name // Display the name that was read
ENDWHILE
CLOSEFILE "names.txt" // Close the file
- Appending to a file:
OPENFILE "names.txt" FOR APPEND // Open the file names.txt for appending
WRITELINE "names.txt", "Charlie" // Write "Charlie" to the file and add a newline
CLOSEFILE "names.txt" // Close the file
Exam Focus
- Examiners want to see that you understand the purpose of file handling: persistent storage, data sharing, etc.
- Use the correct terminology:
OPENFILE,READFILE,WRITEFILE,CLOSEFILE,EOF. - You need to be able to write pseudocode to open, read, write, and close files.
- Understand the differences between text and binary files, and
READFILEandREADLINE, andWRITEFILEandWRITELINE. - Explain why you are doing each step in your pseudocode, not just what you are doing. E.g. "Open the file 'data.txt' for writing, so that we can store new data in the file."
- Demonstrate understanding of
EOFand using it within a loop to read until the end of a file.
Common Mistakes to Avoid
❌ Wrong: Writing code that is irrelevant to the question being asked. ✓ Right: Focus only on the file handling aspect of the question.
❌ Wrong: Saying "open the file to retrieve data". ✓ Right: "Open the file to prepare it for reading data". Then use READFILE or READLINE to actually retrieve the data.
❌ Wrong: Forgetting to close the file. ✓ Right: Always include CLOSEFILE filename at the end of your file handling code. Closing releases resources and ensures data is saved correctly.
❌ Wrong: Confusing WRITEFILE with WRITELINE. WRITEFILE writes data without a newline character. WRITELINE adds a newline character.
✓ Right: Consider whether you want the next item written to the file to be on the same line or a new line.
❌ Wrong: Not understanding that WRITE mode overwrites existing files.
✓ Right: Choose APPEND mode if you want to add to an existing file without deleting the content.
Exam Tips
- Read the question carefully to determine whether you need to read, write, or append to a file.
- Always explain your pseudocode clearly, stating the purpose of each step.
- If the question involves reading data from a file, make sure to use a loop with
EOFto process all the data. - Practice writing pseudocode examples for different file handling scenarios: creating a file, adding data, reading and processing data, updating data.