File Handling
Learn it by playing
Answer these questions to earn energy, then fish and explore. No account needed.
Notes
What is File Handling?
- **File handling** uses programming techniques to work with information stored in **text files**.
- Common operations: **open**, **read**, **write**, and **close** files.
- Files provide **permanent storage** for data beyond program execution.
Opening Files
- Use `OPENFILE "filename" FOR READ` (pseudocode) or `open("filename", "r")` (Python) to **open for reading**.
- Use `OPENFILE "filename" FOR WRITE` or `open("filename", "w")` to **open for writing** – creates new file or **overwrites** existing.
- Use `OPENFILE "filename" FOR APPEND` or `open("filename", "a")` to **open for appending** – adds data to end of existing file.
- Always **close** files after use with `CLOSEFILE` or `file.close()`.
Reading from Files
- `READFILE "file", variable` (pseudocode) or `file.readline()` (Python) reads **one line** as a string.
- Use a **loop** with an **end-of-file flag** to read all records.
- `TRIM()` (pseudocode) or `.strip()` (Python) removes extra spaces/newlines from read data.
- Check for empty string to detect end of file.
Writing to Files
- `WRITEFILE "file", data` (pseudocode) or `file.write(data)` (Python) writes a string to the file.
- Add `NEWLINE` (pseudocode) or `"\n"` (Python) to separate records/lines.
- **Overwrite mode** (`"w"`) deletes existing content; **append mode** (`"a"`) preserves it.
Pseudocode Example (Reading)
- Open file for read, set `endOfFile ← FALSE`.
- Loop: read name, department, salary, age; trim each.
- If name is empty, set `endOfFile ← TRUE`; else output details.
- Close file after loop.
Python Example (Reading)
- `file = open("employees.txt", "r")` then loop using `readline().strip()`.
- Check `if name == "":` to set `endOfFile = True`.
- Print fields and close file with `file.close()`.
Pseudocode Example (Writing/Appending)
- Open file for append: `OPENFILE "employees.txt" FOR APPEND`.
- Write each field on its own line, adding `NEWLINE` after each.
- Close file when done.
Python Example (Writing/Appending)
- `file = open("employees.txt", "a")` then `file.write("Polly\n")` etc.
- Close file with `file.close()`.
Examiner Tips & Common Mistakes
- Use correct mode letter: `"r"` for read, `"w"` for write (overwrites), `"a"` for append.
- **Always close files** to avoid data loss or corruption.
- Backup text files before testing – one mistake can lose contents.
Worked Example (CIE Style)
- Input title and year from user.
- Open `books.txt` for write (or append if preserving existing data).
- Write title and year to file, then close.
- Marks: input (1), open (1), write (1), close (1).
File handling operations as a sequence: Open → Read/Write → Close.
Practice questions
Free preview — 8 of 40 questions. Sign up to see them all.
1.What does the 'r' mode in file handling allow you to do?
Easy- ARead from a file only
- BWrite to a new file
- CAppend to an existing file
- DRead and write to a file
2.Which file mode would you use to add data to the end of an existing file without overwriting it?
Easy- A'w'
- B'a'
- C'r'
- D'x'
3.What is the purpose of the CLOSEFILE command in pseudocode?
Easy- ATo delete the file from the disk
- BTo release the file from the program's control
- CTo clear all data in the file
- DTo open the file for reading
4.A program uses the pseudocode statement: OPENFILE "data.txt" FOR WRITE. What happens if "data.txt" already exists?
Medium- AThe program appends data to the end of the file
- BThe program overwrites the existing contents of the file
- CThe program raises an error and stops
- DThe program reads the existing contents first
5.In the following pseudocode, what is the purpose of the variable endOfFile?
Medium- ATo count the number of lines read
- BTo signal when there is no more data to read
- CTo store the last line of the file
- DTo indicate the size of the file
6.A text file contains employee records with each record spread over four lines: name, department, salary, age. Which pseudocode correctly reads one record?
Medium- AREADFILE "employees.txt", name READFILE "employees.txt", department READFILE "employees.txt", salary READFILE "employees.txt", age
- BREADFILE "employees.txt", name, department, salary, age
- COPENFILE "employees.txt" FOR READ READFILE "employees.txt", name, department, salary, age
- DREADFILE "employees.txt", record SPLIT record INTO name, department, salary, age
7.Consider the pseudocode: OPENFILE "data.txt" FOR READ WHILE NOT EOF READFILE "data.txt", line IF line = "" THEN BREAK ENDIF OUTPUT line ENDWHILE CLOSEFILE "data.txt" What is the purpose of the IF statement checking for an empty line?
Hard- ATo skip blank lines in the file
- BTo detect the end of the file
- CTo count the number of lines
- DTo remove empty lines from the output
8.Which Python statement correctly opens a file named 'students.txt' for appending?
Medium- Afile = open('students.txt', 'a')
- Bfile = open('students.txt', 'w')
- Cfile = open('students.txt', 'r')
- Dfile = open('students.txt', 'append')
Unlock all 40 questions, slides & more
Create a free account to see every question, the slides, flashcards and revision notes for this topic.
Past papers
Past-paper practice for this topic is coming soon.