November 16, 2026•5 min read

Pascal File I/O: Why the Second Save Erased the First Score

Two saves, one line left in the file, and exit status 0. rewrite empties the file the moment you call it, so the fix is append behind a FileExists test, plus a close so the next program can read what you wrote.

Read in Indonesian

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

  • rewrite empties a file when it is called, so a second save leaves one line in it and reports success.
  • append opens an existing file for writing at its end, and FileExists decides between append and rewrite.
  • reset and append on a missing file stop the program, with Runtime error 2 in a plain program and EInOutError once SysUtils is linked.
  • close writes 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.

PASCAL
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.

Output
$ 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.

PASCAL
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.

Output
$ 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.

PASCAL
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.
Output
$ 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.

Output
$ 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.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
Struktur DataWhat a Linked List Actually Is

A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.

PascalPascal Capstone: Why Every Gradebook Score Came Back Zero

A gradebook reads three students from a file, prints three rows, and every score is 0. The procedure filled a copy of the array, because the parameter was missing var. Add the word and the report matches the file; the compiler had already warned about the call that filled nothing.

For developers who learn by struggling

© 2026 Inva.dev.