i'm getting an I/o 998 error, my task is to rewrite numbers from file to array, and find max and min values. What i'm doing wrong ?
implementation
var
f2: file of Real;
m: array of Real;
procedure TForm1.Button1Click(Sender: TObject);
var
f: Real;
max, min: Real;
i, j: Integer;
begin
AssignFile(F2, 'test3.dat');
Rewrite(f2);
for i := 1 to 50 do
begin
f := RandomRange(-100, 100);
Randomize;
Write(f2, f);
end;
CloseFile(f2);
i := 0;
Reset(f2);
while not Eof(f2) do
begin
SetLength(m, i);
Read(f2, m[i]);
Inc(i);
end;
CloseFile(f2);
max := m[1];
min := m[1];
for j := 1 to i do
if m[j] > max then
max := m[j]
else
if m[j] < min then
min := m[i];
The above code sets the length of a dynamic array to 0 (
i
) and tries to read into its non-existing element. This causes the RTL to pass an invalid buffer toReadFile
api. The OS returns '0' indicating the function failed and sets the last error to '998' - that's ERROR_NOACCESS. RTL sets the in/out error code and raises it.As for the answer, use the debugger. Break when the debugger raises an exception. On the next run, put a breakpoint on the faulting statement then trace into code (RTL in this case). Additionally, should you have 'range checking' on in compiler options, you'd get a range check error instead of an I/O error, in which case you would probably see the mistake quickly.