Calling save(10) then save(20) leaves only 20 in results.txt. The program prints no error and exits with status 0, while the first score is gone.
Key takeaways
rewriteempties a file when it is called, so a secondsaveleaves one line in it and reports success.appendopens an existing file for writing at its end, andFileExistsdecides betweenappendandrewrite.resetandappendon a missing file stop the program, withRuntime error 2in a plain program andEInOutErroronceSysUtilsis linked.closewrites the buffer to disk, and without it the file stays 0 bytes and the next program reads nothing.
The save that overwrites
Here is the starter, save_scores.pas. It calls save twice, then show to print the file.
program save_scores;
var
scores: text;
procedure save(score: integer);
begin
assign(scores, 'results.txt');
rewrite(scores);
writeln(scores, score);
close(scores);
end;
procedure show;
var
score: integer;
begin
assign(scores, 'results.txt');
reset(scores);
while not eof(scores) do
begin
readln(scores, score);
writeln(score);
end;
close(scores);
end;
begin
save(10);
save(20);
show;
end.
The commands below use fpc -vw, which prints nothing when the compile succeeds. Warnings and errors still show. Each run starts by deleting results.txt, so you never read yesterday's lines.
$ rm -f results.txt && fpc -vw save_scores.pas && ./save_scores
20
The exit status is 0, and results.txt is 3 bytes holding 20. The 10 that save(10) wrote is not in the file. The second call to save ran rewrite, which emptied the file before the 20 went in.
A checker turns that symptom into a number. It reads the lines the program prints and compares them with what the file should hold.
program check_save_scores;
{ Reads the lines save_scores prints and compares them with what the file should hold. }
uses SysUtils;
const
expected: array[1..2] of integer = (10, 20);
var
line: string;
value: integer;
code: integer;
index: integer;
failures: integer;
begin
index := 1;
failures := 0;
while not eof(input) do
begin
readln(line);
if Trim(line) = '' then
continue;
if index > Length(expected) then
begin
writeln('FAIL extra line: ', line);
Inc(failures);
Inc(index);
continue;
end;
Val(Trim(line), value, code);
if (code <> 0) or (value <> expected[index]) then
begin
writeln('FAIL line ', index, ': expected ', expected[index], ', got ', Trim(line));
Inc(failures);
end
else
writeln('PASS line ', index, ': ', value);
Inc(index);
end;
if index - 1 < Length(expected) then
begin
writeln('FAIL the file holds ', index - 1, ' of ', Length(expected), ' scores');
Inc(failures);
end;
writeln;
writeln(Length(expected) - failures, ' of ', Length(expected), ' checks passed');
if failures > 0 then
ExitCode := 1;
end.
Pipe the program into it. The checker compiles with fpc -vw check_save_scores.pas once.
$ rm -f results.txt && ./save_scores | ./check_save_scores
FAIL line 1: expected 10, got 20
FAIL the file holds 1 of 2 scores
0 of 2 checks passed
The checker exits with status 1.
rewrite is a delete
assign only connects a file variable to a name. rewrite creates the file or empties it, and it does so when you call it, not when you write. A file of 8 bytes went to 0 bytes after assign plus rewrite plus close, with nothing written in between.
append opens an existing file for writing at its end. It adds lines where rewrite replaces them. reset opens an existing file for reading, which is what show uses. The loop in show ends when eof is true, which means the read position is at the end of the file.
The fix: ask FileExists first
The fixed program adds uses SysUtils and tests FileExists before it opens the file. When the file is there, append adds to it. When it is not, rewrite creates it.
program save_scores;
uses SysUtils;
var
scores: text;
procedure save(score: integer);
begin
assign(scores, 'results.txt');
if FileExists('results.txt') then
append(scores)
else
rewrite(scores);
writeln(scores, score);
close(scores);
end;
procedure show;
var
score: integer;
begin
assign(scores, 'results.txt');
reset(scores);
while not eof(scores) do
begin
readln(scores, score);
writeln(score);
end;
close(scores);
end;
begin
save(10);
save(20);
show;
end.
$ rm -f results.txt && fpc -vw save_scores.pas && ./save_scores
10
20
The exit status is 0, and results.txt is 6 bytes holding 10 and 20.
$ rm -f results.txt && ./save_scores | ./check_save_scores
PASS line 1: 10
PASS line 2: 20
2 of 2 checks passed
The checker exits with status 0.
The traps
reset and append both need a file that exists. In a program with no uses SysUtils, reset on a missing file stops with Runtime error 2 at $00000000004010D2, followed by two stack addresses. The exit status is 2. In a program that does use SysUtils, the same mistake arrives as EInOutError: File not found under An unhandled exception occurred at $00000000004010CD:. The exit status is 217.
A missing close bites from the other side. A program that writes three lines with rewrite and never calls close exits 0 and leaves a 0-byte file behind. close writes the buffer to disk, so the lines never arrive. A second program that opens the file for reading prints nothing.
For an exercise, delete the if FileExists test from the fixed program and always call append. Run it in a fresh directory. The program stops before printing anything, with EInOutError: File not found and exit status 217, so the checker receives no lines at all.