Six original Cambridge-style questions on text file handling in 9618 pseudocode, with trace tables and mark points.
WHILE NOT EOF(file), READFILE file, StringVar, process, ENDWHILE.Explain why a program that keeps a list of high scores needs to store them in a file.
Describe the effect of opening a text file in each of these modes: READ, WRITE, APPEND.
Stock.txt contains five lines: Pen, Ink, (blank line), Pad, Pin. State the output of this pseudocode.
Count ← 0
OPENFILE "Stock.txt" FOR READ
WHILE NOT EOF("Stock.txt")
READFILE "Stock.txt", Item
IF Item <> "" THEN
IF MID(Item, 1, 1) = "P" THEN
OUTPUT Item
ENDIF
ELSE
Count ← Count + 1
ENDIF
ENDWHILE
CLOSEFILE "Stock.txt"
OUTPUT CountNames : ARRAY[1:30] OF STRING is a global array; unused elements hold the empty string. Write pseudocode for a procedure SaveNames() that writes every non-empty name to a new file Names.txt, one per line.
PROCEDURE SaveNames() ... ENDPROCEDURE. ✓OPENFILE "Names.txt" FOR WRITE before the loop. ✓FOR Index ← 1 TO 30 ... NEXT Index. ✓IF Names[Index] <> "" THEN WRITEFILE "Names.txt", Names[Index]. ✓CLOSEFILE "Names.txt" after the loop. ✓Words.txt contains an unknown number of lines. Write pseudocode to read the lines into Words : ARRAY[1:100] OF STRING, stopping at the end of the file or when the array is full, and then output how many lines were stored.
Count ← 0 and a STRING variable for the line. ✓OPENFILE "Words.txt" FOR READ. ✓WHILE NOT EOF("Words.txt") AND Count < 100. ✓READFILE "Words.txt", LineIn, then Count ← Count + 1. ✓Words[Count] ← LineIn inside the loop, after the increment. ✓CLOSEFILE "Words.txt" and OUTPUT Count after the loop. ✓A program adds one line to Visitors.txt each time a visitor signs in. It uses OPENFILE "Visitors.txt" FOR WRITE. At the end of the day the file holds only one name. Explain why and describe how to correct the program.
Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.