Write correct pseudocode to handle text files that contain one or more lines, using the Cambridge 9618 conventions for opening, reading, writing, appending, detecting end‑of‑file (EOF) and closing files.
All file operations are performed through a file handle. The handle is created with OPEN … AS and released with CLOSE. Never forget to close a file – even if the programme exits early (e.g. after an error) the CLOSE statement must be executed to flush buffers and release the lock.
IF FILEEXISTS(fileName) THEN …RETURN after closing any already‑opened handles.| Operation | Pseudocode Syntax | Description |
|---|---|---|
| Open for reading | OPEN fileName FOR READ AS file | Creates a handle file for sequential reading. |
| Open for writing | OPEN fileName FOR WRITE AS file | Creates a new file (or overwrites an existing one) for sequential writing. |
| Open for appending | OPEN fileName FOR APPEND AS file | Opens an existing file and positions the pointer at the end. |
| Read a line | READLINE file INTO line | Reads the next line of text; if no more lines are available the EOF flag becomes true. |
| Write a line | WRITELINE file, line | Writes line followed by a newline character. |
| Test for end‑of‑file | EOF(file) | Returns TRUE when the file pointer is positioned after the last line. |
| Close a file | CLOSE file | Flushes buffers and releases the handle. |
| Check existence | FILEEXISTS(fileName) | Returns TRUE if the named file is present on disk. |
/*--- Template: read a text file line‑by‑line ---*/
OPEN "yourFile.txt" FOR READ AS f
WHILE NOT EOF(f) DO
READLINE f INTO currentLine
/* Process currentLine here */
END WHILE
CLOSE f
/* If you need to exit early, remember:
IF someCondition THEN
CLOSE f // always close before RETURN/EXIT
RETURN
END IF
*/
OPEN "data.txt" FOR READ AS f
WHILE NOT EOF(f) DO
READLINE f INTO currentLine
// e.g. count words, validate format, etc.
END WHILE
CLOSE f
OPEN "output.txt" FOR WRITE AS f
FOR i FROM 1 TO n DO
line ← "Record " & i
WRITELINE f, line
END FOR
CLOSE f
OPEN "log.txt" FOR APPEND AS f
WRITELINE f, "Program started at " & CURRENTTIME()
CLOSE f
lineCount ← 0
OPEN "input.txt" FOR READ AS f
WHILE NOT EOF(f) DO
READLINE f INTO line
lineCount ← lineCount + 1
// Additional processing here
END WHILE
CLOSE f
/* lineCount now holds the total number of lines (L) in the file */
FILEEXISTS before attempting to read a file that may be missing.CLOSE after the last operation, even inside conditional branches or before an early RETURN.EOF(file) before calling READLINE.WRITELINE automatically adds a newline; do not add an extra “\n”.FILEEXISTS and open only the necessary mode.sum ← 0
count ← 0
OPEN "numbers.txt" FOR READ AS f
WHILE NOT EOF(f) DO
READLINE f INTO line
value ← STRINGTOINT(line)
sum ← sum + value
count ← count + 1
END WHILE
CLOSE f
IF count > 0 THEN
average ← sum / count
ELSE
average ← 0
END IF
/* sum and average now contain the required results */
sum ← 0
count ← 0
evenCount ← 0
OPEN "numbers.txt" FOR READ AS f
WHILE NOT EOF(f) DO
READLINE f INTO line
value ← STRINGTOINT(line)
sum ← sum + value
count ← count + 1
IF value MOD 2 = 0 THEN
evenCount ← evenCount + 1
END IF
END WHILE
CLOSE f
IF count > 0 THEN
average ← sum / count
ELSE
average ← 0
END IF
/* sum, average and evenCount now hold the results */
OPEN "numbers.txt" FOR READ AS inFile
OPEN "squares.txt" FOR WRITE AS outFile
WHILE NOT EOF(inFile) DO
READLINE inFile INTO line
value ← STRINGTOINT(line)
square ← value * value
WRITELINE outFile, line & " " & STRING(square)
END WHILE
CLOSE inFile
CLOSE outFile
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.