File Handling
Saving data so it still exists after the program closes.
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
//The pattern to memorise
Open, use, close — always close the file. Note the different modes: READ, WRITE (starts a new file) and APPEND (adds to the end).
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", "Bat,78"
CLOSEFILE "Scores.txt"
OPENFILE "Scores.txt" FOR READ
WHILE NOT EOF("Scores.txt") DO
READFILE "Scores.txt", LineOfText
OUTPUT LineOfText
ENDWHILE
CLOSEFILE "Scores.txt"//Why EOF matters
EOF stands for end of file. Without checking it, the program tries to read past the last line and crashes. Use it as the loop condition whenever you read an unknown number of lines.
CHECK YOURSELF
1.Why must you check for EOF when reading a file?
2.You want to add a new record without deleting what is already in the file. Which mode?
3.Why store data in a file rather than only in variables?