runtime-errorpascalfreepascal

What does FreePascal run‐time error 104 mean?


I’m very new to using files and I’m really struggling to fix this. Any help would be great. It seems that the error is coming from my ReadArray function but not entirely. Certainly, I am also not to sure what this 104 error really means.

program ReadFromFile;

type
    lineArray = array [0..19] of String;

procedure PrintArray(lines: lineArray);
var
    i: Integer;
begin
    for i:=0 to High(lines) do
    begin
        WriteLn('Text is: ', lines[i], ' Line number is: ', i);
    end;
end;

function ReadArray(var myFile: TextFile):lineArray;
var 
    count : Integer;
    lines : lineArray;
    i: Integer;
begin
    ReadLn(myFile, count);
    for i := 0 to count do
    begin
        ReadLn(myFile, lines[i]);
    end;
    result := lines;
end;

procedure Main();
var
    myFile: TextFile;
    line: lineArray;
begin

    AssignFile(myFile, 'mytestfile.dat');
    ReWrite(myFile);
    line:=ReadArray(myFile);
    Close(myFile);

    AssignFile(myFile, 'mytestfile.dat');
    Reset(myFile);
    PrintArray(line);
    Close(myFile);
end;

begin
  Main();
end.

Solution

  • You don't know what that error means. Neither do I off the top of my head. So, let's look it up in the documentation and find out. Websearch takes us here: https://www.freepascal.org/docs-html/user/userap4.html

    File not open for input

    Reported by Read, BlockRead, Eof, Eoln, SeekEof or SeekEoln if the file is not opened with Reset.

    You have your calls to open the file the wrong way round. Call Reset to open for reading, Rewrite to open for writing.

    Notes: