File Handling, Knowledge Organiser

Knowledge Organiser: File Handling

Key Terms

  • File: A named collection of data stored permanently on secondary storage
  • open(): Opens a file so it can be read or written
  • readLine(): Reads the next line from an open file
  • writeLine(): Writes a line of text to the end of a file
  • endOfFile(): Returns true when there are no more lines to read
  • close(): Closes a file after use — must always be called

Must-Know Facts

  • Variables are temporary; files provide permanent storage that survives program shutdown
  • Files must always be closed after use — leaving them open can cause data loss
  • In OCR style, writeLine() adds the line to the end of the file
  • Use endOfFile() in a WHILE loop to read all lines without going past the end
  • The three steps are: Open, Read/Write, Close (ORC)

Key Concepts

  • Open for reading: myFile = open("data.txt")
  • Read all lines: while NOT myFile.endOfFile() ... line = myFile.readLine() ... endwhile
  • Write to file: myFile = open("out.txt") ... myFile.writeLine("text")
  • Always close: myFile.close()

Common Mistakes

  • Forgetting to close the file: Not calling close() after reading/writing can cause data loss or file corruption — always close as the final step
  • Opening a file but never using the variable: open() returns the file, so store it in a variable (myFile = open("data.txt")) and call readLine(), writeLine() and close() on that variable
  • Not using endOfFile() in a loop: Reading past the end of a file causes a runtime error — always use a WHILE NOT endOfFile() loop to read all lines safely
  • Thinking variables and files work the same way: Variables are temporary (lost when the program ends); files are permanent storage on secondary storage — a key distinction examiners test
  • Writing pseudocode file handling without open/close: Exam mark schemes require open, read/write, and close — missing either the open or close step loses marks

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.