📘 CIE IGCSE Computer Science
💾 File Handling – Detailed Notes
1️⃣ Purpose of Storing Data in a File
🔹 What is a File?
A file is a collection of data stored on secondary storage (e.g. hard disk, SSD) so that it can be accessed later.
Unlike variables:
- Variables store data temporarily (RAM)
- Files store data permanently (secondary storage)
🔹 Why Do Programs Use Files?
Programs store data in files to:
✔ Save data permanently
✔ Use data again later
✔ Share data between programs
✔ Store large amounts of data
✔ Keep records (students, products, scores)
🔹 Example Situations
- Saving student marks
- Storing login usernames/passwords
- Saving high scores in a game
- Recording transactions
🔹 Example Scenario
Without file:
INPUT name
If program closes → name is lost.
With file:
- Name is written to file
- Can be read next time program runs
🧠 Exam Tip
If question asks:
"Why is a file used?"
You should mention:
- Permanent storage
- Data reuse
- Data persistence after program ends
2️⃣ Opening, Closing and Using Files
Files must be:
- Opened
- Used (read/write)
- Closed
🔹 Opening a File
For Reading:
OPENFILE "Students.txt" FOR READ
For Writing:
OPENFILE "Students.txt" FOR WRITE
⚠️ Important:
- READ → Reads existing data
- WRITE → Overwrites file (deletes old data)
- APPEND (sometimes used) → Adds to file
🔹 Closing a File
Always close the file:
CLOSEFILE "Students.txt"
🧠 Why Close Files?
- Frees system resources
- Prevents data corruption
- Ensures data is properly saved
3️⃣ Reading and Writing Single Items
🔹 Writing a Single Item
OPENFILE "Marks.txt" FOR WRITE
WRITEFILE "Marks.txt", 75
CLOSEFILE "Marks.txt"
🔹 Reading a Single Item
OPENFILE "Marks.txt" FOR READ
READFILE "Marks.txt", mark
OUTPUT mark
CLOSEFILE "Marks.txt"
4️⃣ Writing and Reading a Line of Text
🔹 Writing a Line
OPENFILE "Names.txt" FOR WRITE
WRITEFILE "Names.txt", "Austin"
CLOSEFILE "Names.txt"
🔹 Reading a Line
OPENFILE "Names.txt" FOR READ
READFILE "Names.txt", name
OUTPUT name
CLOSEFILE "Names.txt"
5️⃣ Reading Multiple Lines Using Iteration
Very common in exams.
🔹 Example File Content
Marks.txt:
45
67
89
32
🔹 Reading All Values
🔹 What is EOF?
EOF = End Of File
Used to detect when file has no more data.
🧠 Exam Tip
Use:
WHILE NOT EOF(filename)
To prevent reading past the end.
6️⃣ Writing Multiple Items Using Iteration
OPENFILE "Numbers.txt" FOR WRITE
FOR i ← 1 TO 5
INPUT number
WRITEFILE "Numbers.txt", number
NEXT i
CLOSEFILE "Numbers.txt"
7️⃣ File Handling + Totalling Example
Very common exam question:
Question:
Read numbers from a file and output total.
Answer:
8️⃣ File Handling + Counting Example
9️⃣ Common Exam Questions
You may be asked to:
✔ Complete missing file code
✔ Identify errors
✔ Explain why file is needed
✔ Write code to read/write data
✔ Combine file + arrays
✔ Combine file + selection
🔟 Very Common Mistakes
❌ Forgetting to open file
❌ Forgetting to close file
❌ Using WRITE instead of READ
❌ Not using EOF
❌ Not initialising totals
❌ Wrong file mode (WRITE instead of READ)
1️⃣1️⃣ Comparison: Files vs Variables
| Feature | Variable | File |
|---|---|---|
| Stored in | RAM | Secondary storage |
| Permanent? | No | Yes |
| Available after program ends? | No | Yes |
| Used for large data? | Limited | Yes |
1️⃣2️⃣ Good Programming Practice (Maintainability)
✔ Use meaningful file names
✔ Comment file purpose
✔ Always close files
✔ Check logic carefully
✔ Keep indentation clean
Example:
// Open file to read student marks
OPENFILE "StudentMarks.txt" FOR READ
🎯 Exam Strategy
When you see a file question:
1️⃣ Identify if reading or writing
2️⃣ Declare variables
3️⃣ Open file correctly
4️⃣ Use loop if multiple data
5️⃣ Close file
6️⃣ Output result if needed
⭐ Final Quick Summary
File handling involves:
- Opening a file
- Reading or writing data
- Using loops for multiple records
- Checking EOF when reading
- Closing the file