j.cole j.cole - 10 months ago 164
Pascal Question

Runtime error 104 Pascal

Im very new to using files and im really struggling to fix this any help would be great.
It seems that the error is coming from my read array function but not entirely
sure i am also not to sure what the 104 error really means
thanks in advance

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.

Answer Source

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:

  • Looping from 0 to count will perform count + 1 iterations. I'd expect to see you looping from 0 to count - 1.
  • You don't check whether your array is long enough. You therefore run the risk of a buffer overrun. A dynamic array would avoid this.
  • It's not clear why you open the file for a second time when you print the contents to the console.
  • You could have looked up the error code yourself. Please take the hint to do web search the next time you encounter an error like this.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download