No you did not test it. You did not test:
- a File does not exist (IOResult on reset)
- a File is empty (0) (IOResult same, on some platforms eof by defaultr)
Yes, I tested all of it. If a file is empty,
IOResult returns
0 after
Reset. If the source file not exists,
IOResult returns nonzero error code (so it has to be), so entering the
repeat loop and trying to read the data is pointless.
var
Input: Text;
Line: String;
begin
{$I-}
Assign(Input, 'C:\file.txt');
Reset(Input);
WriteLn('Reset code: ', IOResult(), LineEnding);
while not EoF(Input) do
begin
ReadLn(Input, Line);
WriteLn('"', Line, '"');
end;
Close(Input);
WriteLn(LineEnding, 'Close code: ', IOResult());
end.
The output is:
Reset code: 0
Close code: 0
But with
repeat loop:
var
Input: Text;
Line: String;
begin
{$I-}
Assign(Input, 'C:\file.txt');
Reset(Input);
WriteLn('Reset code: ', IOResult(), LineEnding);
repeat
ReadLn(Input, Line);
WriteLn('"', Line, '"');
until EoF(Input);
Close(Input);
WriteLn(LineEnding, 'Close code: ', IOResult());
end.
the output is:
Reset code: 0
""
Close code: 0
As you can see, the loop iterate ones, loading and processing an empty string. But if the source file not exists, in both cases the output is the same:
Don't know why only the error codes, but it is not important now. Tested few minutes ago under Windows. So, if the
repeat loop is recommended, how to detect the void? I'm asking seriously.
If I need to test if the file exists, I can use
FileExists function. Or just check the
IOResult:
{$I-}
Assign(Input, 'C:\file.txt');
Reset(Input);
if IOResult() = 0 then // prevents reading from a non-existent file
begin
while not EoF(Input) do // prevents reading from an empty file
begin
ReadLn(Input, Line);
WriteLn('"', Line, '"');
end;
Close(Input); // closes the file only if it has been opened
end;