Open, close and use a file for reading and writing

Programming – Opening, Closing and Using Files (IGCSE 0478)

1. Why Use Files?

  • Permanent storage – data remains after the program stops.
  • Data sharing – different runs or different programmes can read the same record file.
  • Large data sets – files can hold far more information than can be kept in RAM.

2. Key Terminology (Cambridge wording)

  • File – a collection of records stored on secondary storage.
  • File handle – the variable that represents an opened file.
  • Mode – tells the computer how the file will be accessed (read, write, append, …).
  • Record – one line of data in a text file (e.g. Bob,78).

3. File Modes (Cambridge‑compatible)

Mode (Python) Cambridge description Typical use
'r'Read‑only; file must already exist.Reading existing records.
'w'Write‑only; creates a new file or empties an existing one.Saving fresh data, overwriting old content.
'a'Append‑only; writes are added to the end of the file.Adding log entries.
'r+'Read‑write; file must exist.Updating particular records.
'w+'Read‑write; creates a new file or truncates an existing one.Re‑creating a file with new content.
'a+'Read‑append; reads from the start, writes at the end.Reading a log while adding new entries.

4. Cambridge Pseudocode for File Operations

Operation Cambridge keyword Example (pseudocode)
Open a fileOPENOPEN scoresFile ← "scores.txt", "r"
Close a fileCLOSECLOSE scoresFile
Read a lineREADREAD line FROM scoresFile
Write a lineWRITEWRITE line TO scoresFile
Append a lineAPPENDAPPEND line TO scoresFile
Read entire fileREADALL (exam‑style)READALL data FROM scoresFile

5. Opening and Closing a File (Python example)

# Open the file for reading – obtain a file handle called f
f = open('scores.txt', 'r')

# …perform operations…

# Always close when finished
f.close()

In the exam you can replace the above with the Cambridge pseudocode shown in the table.

6. Reading from a File

  • read() – returns the whole file as one string.
  • readline() – returns the next line (including the newline character).
  • readlines() – returns a list of all lines.
  • Iterating directly over the file object reads line‑by‑line automatically.

Python example (line‑by‑line)

with open('scores.txt', 'r') as f:          # ← automatically closes
    for line in f:                         # reads one record at a time
        process(line)                      # user‑defined routine

Cambridge‑style pseudocode

OPEN scoresFile ← "scores.txt", "r"
WHILE NOT EOF(scoresFile) DO
    READ line FROM scoresFile
    // process the line, e.g. split into name and mark
ENDWHILE
CLOSE scoresFile

7. Writing to a File

  • write(string) – writes exactly the characters supplied.
  • writelines(list) – writes each element of a list in order.
  • When using mode 'a' or 'a+' the write position is always at the end of the file.

Python example (appending a log entry)

with open('log.txt', 'a') as f:
    f.write('User logged in at 12:34')

Cambridge‑style pseudocode

OPEN logFile ← "log.txt", "a"
APPEND "User logged in at 12:34" TO logFile
CLOSE logFile

8. Integrated Example – Updating Student Scores (Arrays + Loops)

This example demonstrates the full cycle: open → read → store in an array → modify → write → close. It follows the exact Cambridge syntax that can be reproduced in Paper 2.

Python implementation (for reference)

# 1. Read all records into a list of tuples
with open('scores.txt', 'r') as f:
    records = [line.strip().split(',') for line in f]

# 2. Convert to a dictionary for easy update (acts like an array of records)
scores = {name: int(mark) for name, mark in records}

# 3. Update a student's mark
scores['Alice'] = 85

# 4. Write the updated data back – overwrite the file
with open('scores.txt', 'w') as f:
    for name, mark in scores.items():
        f.write(f'{name},{mark}\')

Cambridge‑style pseudocode (exam‑ready)

// ---------------------------------------------------
// 1. READ all records into an array called scoresArray
// ---------------------------------------------------
OPEN inFile ← "scores.txt", "r"
SET i ← 0
WHILE NOT EOF(inFile) DO
    READ line FROM inFile
    SPLIT line BY "," INTO name, mark
    SET scoresArray[i] ← name            // element 0 = name
    SET scoresArray[i+1] ← INTEGER(mark) // element 1 = mark
    ADD 2 TO i                            // each record occupies two cells
ENDWHILE
CLOSE inFile

// ---------------------------------------------------
// 2. UPDATE Alice's mark (example of a write operation)
// ---------------------------------------------------
SET j ← 0
WHILE j < i DO                         // scan the array
    IF scoresArray[j] = "Alice" THEN
        SET scoresArray[j+1] ← 85      // new mark
    ENDIF
    ADD 2 TO j
ENDWHILE

// ---------------------------------------------------
// 3. WRITE the whole array back to the file (overwrite)
// ---------------------------------------------------
OPEN outFile ← "scores.txt", "w"
SET k ← 0
WHILE k < i DO
    SET line ← scoresArray[k] + "," + STRING(scoresArray[k+1]) + NEWLINE
    WRITE line TO outFile
    ADD 2 TO k
ENDWHILE
CLOSE outFile

9. Common Errors – Linked to Assessment Objectives

  1. File does not exist (read mode) – AO2
    Check: Use 'r' only when you are sure the file is present, or add a test such as IF FILEEXISTS(...) in your pseudocode.
  2. Forgot to close the file – AO3 (programming practice)
    Check: Every OPEN must have a matching CLOSE. In the exam, write the CLOSE statement on a new line.
  3. Using the wrong mode (e.g., 'w' instead of 'a') – AO2
    Check: Decide whether you need to preserve existing data. Choose 'a' or 'r+' for “add‑only” tasks.
  4. Mixing binary and text modes – AO2
    Check: For plain‑text records always omit the 'b' suffix.
  5. Incorrect handling of new‑line characters – AO2
    Check: When writing a line, remember to add (or NEWLINE in pseudocode) unless the specification says otherwise.

10. Good Programming Practice (Maintainable Code)

  • Use meaningful identifiers – e.g. scoresFile, studentName, markArray.
  • Include comments (Cambridge uses //) to explain each step.
    // Load all student records into an array
    OPEN inFile ← "scores.txt", "r"
    ...
    
  • Keep the open‑read‑write‑close sequence clear and indented for readability.

11. Summary – The File‑Handling Cycle

  1. OPEN the file with the correct mode.
  2. READ or WRITE/APPEND using the appropriate command.
  3. Process data with variables, arrays, loops as required.
  4. CLOSE the file to flush buffers and free resources.

Remember: the exam expects the Cambridge keywords (OPEN, READ, WRITE, APPEND, CLOSE) and a clear, commented structure.

12. Additional Syllabus Topics (Exam‑Relevant Highlights)

12.1 Data Representation

  • Binary, hexadecimal, two’s‑complement
    • Example: Convert –18 to an 8‑bit two’s‑complement value.
      18 → 0001 0010 (binary) → invert → 1110 1101 → add 1 → 1110 1110
    • Overflow detection: adding two 8‑bit signed numbers and checking the sign bit.
  • Logical shifts – left shift multiplies by 2, right shift divides by 2 (integer division).

12.2 Multimedia Files

Media typeKey parametersTypical exam calculation
Audio (PCM) Sample‑rate, bit‑depth, channels File size = sample‑rate × bit‑depth × channels × duration ÷ 8
Image (bitmap) Resolution, colour depth File size = width × height × colour‑bits ÷ 8

Worked example – audio: 5 s of mono sound at 44.1 kHz, 16‑bit.
5 × 44 100 × 16 ÷ 8 = 5 580 000 bytes ≈ 5.3 MB

Worked example – image: 1024 × 768 pixels, 24‑bit colour.
1024 × 768 × 24 ÷ 8 = 2 359 296 bytes ≈ 2.3 MB

12.3 Data Compression

MethodLossy / LosslessTypical compression ratio
JPEG (image)Lossy10 : 1 – 20 : 1
PNG (image)Lossless2 : 1 – 3 : 1
MP3 (audio)Lossy10 : 1 – 12 : 1
FLAC (audio)Lossless2 : 1 – 3 : 1
ZIP (general)Lossless2 : 1 – 5 : 1

Exam tip: Choose a lossless method when the original data must be reproduced exactly (e.g., medical images).

12.4 Data Transmission

  • Packet structure – Header, Payload, Trailer. The header contains source/destination addresses and error‑checking bits; the trailer often holds a CRC.
  • USB – serial bus, host‑controlled, supports plug‑and‑play.
  • Error detection – parity bit, checksum, CRC. Example: an 8‑bit parity check adds a single bit so that the total number of 1’s is even.
  • Encryption (basic) – Caesar cipher, XOR with a key, or symmetric block ciphers (AES). In the exam you may be asked to encode/decode a short message.

12.5 Hardware Overview

  • CPU & FDE cycle – Fetch, Decode, Execute. The CPU reads an instruction from memory (Fetch), determines what it means (Decode), and performs the required operation (Execute).
  • Multi‑core & cache – Each core has its own registers; cache stores frequently used data close to the CPU to speed up access.
  • Embedded systems – Dedicated computers (e.g., micro‑controllers) that perform a single function.
  • Von Neumann vs Harvard architecture – Von Neumann uses a single memory for data and instructions; Harvard separates them, allowing simultaneous access.

12.6 Software Concepts

  • System software vs application software – OS, drivers, utilities versus programmes that solve specific user problems.
  • Operating‑system functions – file management, memory management, process scheduling, device control.
  • Interrupts – hardware or software signals that cause the CPU to pause the current task and execute an interrupt‑service routine (ISR).
    // Simple ISR for a keyboard press (pseudocode)
    WHEN KEYBOARD_INTERRUPT DO
        READ key FROM keyboardPort
        STORE key IN buffer
    ENDWHEN
    
  • Integrated Development Environments (IDEs) – combine editor, compiler/interpreter, debugger.

12.7 Internet & World‑Wide Web

  • URL structure – protocol, domain, path, query string.
  • HTTP vs HTTPS – HTTPS adds TLS encryption for secure data transfer.
  • Cookies – small text files stored on the client to maintain state (e.g., session ID).
  • DNS – translates a domain name to an IP address before the HTTP request is sent.

12.8 Digital Currency & Blockchain

  • Blockchain block diagramHash | Previous‑Hash | Data | Timestamp. Each block’s hash depends on the previous block, making tampering difficult.
  • Security benefit: altering a transaction changes its hash, which would break the chain and be detected by the network.

12.9 Cyber‑Security Threats & Counter‑Measures

  • Malware types – viruses, worms, trojans, ransomware.
  • Social engineering – e.g., phishing emails that trick users into revealing passwords.
  • Case study (phishing) – A student receives an email appearing to be from the school, asking for the login ID.
    Mitigation strategies (AO 3):
    1. Educate users to verify the sender’s address and never click unknown links.
    2. Implement two‑factor authentication so a stolen password alone is insufficient.
  • Counter‑measures – firewalls, anti‑virus software, strong passwords, regular updates, encryption.

12.10 Emerging & Automated Technologies

  • Sensors & actuators – convert physical quantities to electrical signals and vice‑versa (e.g., temperature sensor, motor).
  • Robotics – combine sensors, actuators, and control programmes to perform tasks.
  • Artificial Intelligence
    • Expert systems – rule‑based decision making.
    • Machine learning – algorithms that improve from data.
    • Neural networks – layers of interconnected “neurons” that model complex patterns (useful for image recognition).
  • Automation in industry – PLCs (Programmable Logic Controllers) control assembly lines, using input from sensors and output to actuators.

13. Exam‑Style Practice Questions (selected)

  1. Write Cambridge pseudocode to open "data.txt" for appending, add the line "New entry", and close the file.
  2. Convert –18 to an 8‑bit two’s‑complement binary number.
  3. A 3‑minute mono audio clip is recorded at 48 kHz with 24‑bit depth. Calculate the file size in megabytes.
  4. Explain why a lossless compression method must be used for a medical X‑ray image.
  5. Given the packet header contains a 16‑bit CRC, show how a simple parity error would be detected.
  6. Identify two security risks of using HTTP instead of HTTPS and suggest one mitigation for each.
  7. Briefly describe how a neural network differs from a traditional expert system.

14. Quick Reference Flowchart (text description)

Start → OPEN (choose mode) → Decision: READ or WRITE/APPEND?
If READ: loop READ line until EOF, process data, then CLOSE.
If WRITE/APPEND: prepare data, WRITE or APPEND, then CLOSEEnd.

Create an account or Login to take a Quiz

50 views
0 improvement suggestions

Log in to suggest improvements to this note.