Computer ScienceExam code: 0984
File Handling
Programming
Common mistakes that lose marks (and lose data in real programs):
- Opening a in
WRITEmode when you meant to keep its existing data.WRITEmode overwrites the file, destroying all existing contents. - Forgetting
CLOSEFILE. Buffered data may not reach the disk; the file may be incomplete or corrupt. - Reading past the end of the file without checking for it. There is no
EOFfunction in CIE pseudocode, so always test the line you have just read against a sentinel value and stop the loop with a Boolean flag. - Opening a file that does not exist in READ mode. Causes a runtime error; check it exists first if necessary.
- Mixing modes. A single
OPENFILEopens for one specific mode. To switch between reading and writing, close and reopen the file. - Forgetting to handle blank lines when reading. An empty line is often used as the end-of-file sentinel in the CIE pseudocode pattern, so be careful with files that genuinely contain blank lines in the middle.
- Always make a backup of important text files before writing to them. A program with a bug can wipe out hours of work in milliseconds.