TopMyGrade

GCSE/Computer Science/AQA

CS2.6Input/output and file handling: reading from and writing to text files; reading user input and producing formatted output

Notes

Input/output and file handling

Programs are useful when they communicate with the outside world. They take input (from a user, a file, a sensor) and produce output (to the screen, a file, a printer). On the AQA spec you need to know about user I/O, formatted output, and reading and writing text files.

User input

USERINPUT reads a line from the user. The result is treated as a string by default — so if you need a number, convert it.

USERINPUT name              -- string
SET age TO USERINPUT        -- string
SET ageInt TO STRING_TO_INT(age)

Output

OUTPUT displays text on screen. You concatenate values using +:

OUTPUT "Welcome, " + name
OUTPUT "Total: " + total

In some languages OUTPUT can take multiple arguments — print(name, age, sep=", ").

Formatted output

Pseudocode rarely formats numbers tightly, but in real languages you can pad, round and align:

OUTPUT "Score: " + ROUND(percentage, 1) + "%"

File handling — text files

A text file stores plain characters (lines separated by newlines). To read or write, you open the file, do operations, and close it.

Writing

OPEN file FOR WRITE "scores.txt"
WRITE file "Aisha,87"
WRITE file "Ben,74"
CLOSE file
  • OPEN ... FOR WRITE creates a new file or overwrites an existing one.
  • WRITE appends a single line.
  • CLOSE releases the file handle.

To append instead of overwrite, use OPEN ... FOR APPEND.

Reading

OPEN file FOR READ "scores.txt"
WHILE NOT EOF(file)
  READ file line
  OUTPUT line
ENDWHILE
CLOSE file
  • READ reads the next line (or character or block, depending on language).
  • EOF(file) returns true when the end of the file has been reached.
  • Always close the file when done.

Why file handling matters

  • Persistence. Data outlives a single run of the program — saved settings, scores, logs.
  • Sharing. Files can be moved, emailed, version-controlled.
  • Big data. Reading a file in chunks avoids loading everything into RAM.

CSV (comma-separated values)

A common GCSE pattern: a text file with one record per line, fields separated by commas.

Aisha,87,A
Ben,74,B
Chloe,92,A

To process: read each line, split by ",", store in array or record.

OPEN file FOR READ "scores.csv"
WHILE NOT EOF(file)
  READ file line
  SET parts TO SPLIT(line, ",")
  OUTPUT parts[0] + " scored " + parts[1]
ENDWHILE
CLOSE file

Error handling

Real file operations may fail — file missing, permissions denied, disk full. AQA pseudocode doesn't require try/catch but you should mention checking that the file exists, or that the user has permission, in defensive-programming questions.

Common mistakesPitfalls

  1. Forgetting to close the file — wastes resources, may not flush writes to disk.
  2. Reading past EOF — always test EOF(file) before reading.
  3. Mode confusion — opening for WRITE wipes existing content; opening for APPEND adds at the end.
  4. Treating numbers from a file as integers — they are strings; convert before arithmetic.
  5. Confusing screen output and file outputOUTPUT goes to screen; WRITE file goes to file.

Worked exampleWorked example — counting lines

SET count TO 0
OPEN file FOR READ "log.txt"
WHILE NOT EOF(file)
  READ file line
  SET count TO count + 1
ENDWHILE
CLOSE file
OUTPUT "Total lines: " + count

Try thisQuick check

A file scores.txt has lines like "Aisha,87". Write pseudocode to read each line and output the average score.

SET total TO 0
SET count TO 0
OPEN file FOR READ "scores.txt"
WHILE NOT EOF(file)
  READ file line
  SET parts TO SPLIT(line, ",")
  SET total TO total + STRING_TO_INT(parts[1])
  SET count TO count + 1
ENDWHILE
CLOSE file
IF count > 0 THEN
  OUTPUT "Average: " + (total / count)
ENDIF

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 13 marks

    USERINPUT and conversion

    A pupil writes:

    SET age TO USERINPUT
    SET nextAge TO age + 1
    

    The program errors. Explain why and suggest a fix.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 24 marks

    File write

    Write pseudocode that creates a new text file "names.txt" and writes the names "Anna", "Ben", "Chloe" to it, one per line.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 35 marks

    File read loop

    Write pseudocode that reads every line of "scores.txt" and outputs each line preceded by its line number (1-based).

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 42 marks

    Why close?

    State two reasons it is important to close a file after use.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 54 marks

    CSV split

    A line in scores.csv reads "Aisha,87,A". Show pseudocode that reads this line and outputs the score (87) as an integer.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Mode difference

    Explain the difference between opening a file FOR WRITE and FOR APPEND.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    EOF check

    Why do programs typically check EOF before reading from a file?

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.6 — Input/output and file handling

10-card SR deck for AQA GCSE Computer Science topic CS2.6

10 cards · spaced repetition (SM-2)