Skip to content
Marlbridge

Revision Notes

OxfordAQA IGCSE Computer Science (9210): Input/output and file handling – Revision Notes

Revision notes for OxfordAQA IGCSE Computer Science 9210 section 3.2.7: keyboard input, display output, reading and writing text files, traps and a self-test.

Level
IGCSE
Topic
Topic 2 – Programming
Updated

Aligned to OxfordAQA IGCSE Computer Science (9210), For teaching from September 2017, exams from May/June 2019. Official specification .

Syllabus page (what it covers and how it is assessed): OxfordAQA IGCSE Computer Science.

Syllabus points this page covers

9210

  • 3.2.7 Input/output and file handling

Found an error? Report a correction.

Need help with this topic? Request a free trial class for IGCSE Computer Science (9210).

Condensed notes for section 3.2.7 Input/output and file handling of OxfordAQA International GCSE Computer Science (9210). The worked examples are in the input/output and file handling study guide.

What the specification asks

  • Obtain user input from a keyboard.
  • Output data and information from a program to the computer display.
  • Read from and write to a text file.

Keyboard input

  • name ← USERINPUT stores what the user types.
  • Prompt first: OUTPUT "Enter the quantity".
  • Input is a string: convert before arithmetic, qty ← STRING_TO_INT(USERINPUT).
  • Python: qty = int(input("Enter the quantity: ")).
The display shows the prompt "Enter your age" and the user types 16. USERINPUT arrives as the string "16", which STRING_TO_INT converts to the integer 16, ready for arithmetic. The pseudocode is OUTPUT "Enter your age" followed by age ← STRING_TO_INT(USERINPUT); in Python, age = int(input("Enter your age: ")).
Prompt the user, then convert the string that arrives before doing arithmetic.

Display output

  • OUTPUT sends text or values to the screen.
  • Label values so the output is information, not just data: OUTPUT "Average: " + REAL_TO_STRING(avg).
  • Joining with + needs strings on both sides; convert numbers first.
Two displays: 14.5 on its own is just data, while "Total cost: 14.5" is information because the user knows what the number means. The statement OUTPUT "Total cost: " + REAL_TO_STRING(total) joins fixed text to the number after converting it to a string.
Label every value you output so it becomes information.

Why text files

Variables Text files
Lost when the program ends Kept on secondary storage after the program ends
Only available to this run of the program Can be reused next time and shared with other programs
Hold any data type Hold lines of characters (numbers must be converted)
Variables in a running program lose their values when it ends, but data saved to a text file on secondary storage is still there next time and can be shared with other programs. Typical uses are high scores, event logs, settings and lists of words or questions. Numbers read from a file are strings until converted.
A text file makes data persist after the program ends.

Reading a text file: four steps

  1. Open for reading.
  2. Loop until end of file (WHILE NOT END_OF_FILE(file)), so an empty file gives zero reads.
  3. Read a line and process it (convert if numeric).
  4. Close the file.
A flowchart: open the file for reading, test for the end of the file, and while it is not reached read a line, process it and test again; at the end, close the file. Beside it, pseudocode that totals and counts the numbers in scores.txt and outputs total / count, with a note to check count > 0 so an empty file does not divide by zero.
Open, loop until end of file reading one line at a time, then close.

Writing a text file

Mode Existing contents Typical use
Write Replaced Save a complete set of data, create a new file
Append Kept; new lines added at the end Log entries, adding a new score

Steps: open (choose mode) → write each line (convert numbers to strings) → close. Python modes: "r" read, "w" write, "a" append. Add "\n" at the end of each line when using write.

A log file holds two existing lines. Opened in write mode, it is replaced and holds only the new line. Opened in append mode, the new line is added after the two existing lines. The pseudocode opens log.txt for appending, writes "Login by " + username and closes the file.
Write mode replaces the file; append mode adds to the end.
Pseudocode opens seven.txt in write mode, writes i x 7 = i * 7 for i from 1 to 12, then closes the file, giving 12 lines from 1 x 7 = 7 to 12 x 7 = 84. If the file were opened in write mode inside the loop, each pass would wipe it and only 12 x 7 = 84 would remain.
Open once before the loop and close once after it.

Why close a file

  • Makes sure all data is saved to storage.
  • Releases the file so other programs (or later parts of the same program) can open it.

Template code

file ← OPEN_FILE_READ("words.txt")       -- count lines that start with "s"
count ← 0
WHILE NOT END_OF_FILE(file)
  word ← READ_LINE(file)
  IF SUBSTRING(0, 0, word) = "s" THEN
    count ← count + 1
  ENDIF
ENDWHILE
CLOSE_FILE(file)
OUTPUT "Words starting with s: " + INT_TO_STRING(count)

(File command names are illustrative; any suitable names are accepted if the logic is clear. SUBSTRING(0, 0, word) is the first character, counting from 0.)

The high-score pseudocode in four stages: input the score and convert it to an integer; open highscores.txt in append mode, write the score as a string and close; open the file for reading and loop to the end, converting each line and keeping the best; close the file and output "Best score: " with the result.
Convert, append, close, read every line, close, output: each step earns a mark.

Python equivalents

Task Python
Input a whole number n = int(input("Enter n: "))
Output a labelled value print("Total:", total)
Read every line with open("data.txt", "r") as f: then for line in f:
Add a line to a file with open("log.txt", "a") as f: then f.write(entry + "\n")

Key terms

Term Meaning
Input Data entered into a program, here from the keyboard
Output Data or information sent from a program, here to the display
Prompt A message telling the user what to enter
Text file A file of lines of characters stored on secondary storage
Open Connect a program to a file for reading, writing or appending
End of file The point at which there are no more lines to read
Append Add data to the end of an existing file without removing what is there
Close Finish using a file so data is saved and the file is released

Exam traps

  • Missing conversion of input or file lines before arithmetic.
  • Write mode where append was needed, wiping earlier data.
  • File opened or closed inside the loop.
  • Fixed-count loops for files of unknown length.
  • Forgetting to close the file.
  • Unlabelled output when the question asks for a meaningful message.
  • Concatenating a number with a string without converting it.

Self-test

  1. State why a prompt should be output before USERINPUT.
  2. State the data type of a value entered at the keyboard before conversion.
  3. Give one reason a program saves data to a text file.
  4. List the four steps for reading every line of a text file.
  5. Explain the difference between write mode and append mode.
  6. State two reasons for closing a file.
  7. Why is a WHILE loop with the condition at the start suitable for reading a file?
  8. A program reads "7" from a file and adds it to 3 without converting. What might happen?
  9. Which mode should be used for a log of every login?
  10. Give the Python mode letter for appending.
  11. A program opens a file in write mode, writes one line and closes it, all inside a loop that runs 5 times. How many lines does the file contain at the end?
  12. Rewrite OUTPUT "Score " + score so it works when score is an integer.

Answers:

  1. So the user knows what data to enter (and in what form).
  2. String.
  3. Any one: the data is kept after the program ends; it can be loaded next time; it can be shared with other programs.
  4. Open the file for reading; loop until the end of the file; read and process each line; close the file.
  5. Write mode replaces the file’s existing contents; append mode keeps them and adds new lines at the end.
  6. To make sure the data is saved to storage, and to release the file for other programs.
  7. If the file is empty, the condition is False at the start, so no line is read.
  8. An error, or "73" if the language joins the values as strings, instead of 10.
  9. Append.
  10. "a".
  11. One (each opening wipes the file, so only the last line remains).
  12. OUTPUT "Score " + INT_TO_STRING(score).

More resources on this topic

Related resources

Get free revision emails (optional)

Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.