Repeat
For Counter := 0 to Result Do
begin
Readln(FileVar,A[Counter-1]);
end;
until(EoF(FileVar));
You just love nesting loops too much, don't you?
Think about this:
- Result contains the amount of lines in the file
- "For Counter := 0 to Result Do"
After you read all lines of a file, naturally you are at the end of the file.
- "until(EoF(FileVar));"
So why checking for the end of file, if you know that you must be there?
Even worse, if you where not yet at the end of file, and you would repeat the loop, what would happen?
What if the file had 5 lines, but then another process added one more line, so now it has 6?
The repeat would loop; because only 5 lines where read, so one more to go.
- But it would attempt to read 5 more, ouch.
- And it would overwrite the previously read entries, since Counter starts at 0 again, ouch.
A few other errors in the code:
For Counter := 0 to Result Do
If "Result = 1", how many times will the loop run? "for Counter = 0 to 1" => 2 times.
but you would only have one line in the file, and only one entry in the array. What happens win the 2nd run of the loop?
"A[Counter-1]"
Counter starts at 0. So the first entry in A will be "A[-1]" which is out of range....