Board index » delphi » Reading past eof problem

Reading past eof problem

Why is it that with the following code:

assign(f,FileName);      {a file of records}
reset(f);
while not eof(f)
  read(f,Record);

I on occassion get an 'error 100:Disk Read Error,? Am I trying to read past
the eof? How do I avoid the problem? All help appreciated!

Linden

 

Re:Reading past eof problem


Quote
In article <51c6cm$...@imsai.canuck.com>, <lwil...@canuck.com> writes:
> Why is it that with the following code:

> assign(f,FileName);      {a file of records}
> reset(f);
> while not eof(f)
>   read(f,Record);

> I on occassion get an 'error 100:Disk Read Error,? Am I trying to read past
> the eof? How do I avoid the problem? All help appreciated!

Likely the file you are reading has less than a complete record at the end.  
For instance if your records are 10 bytes, and your file has 12 bytes, then you
will get that error on the second read, but the eof will not be true because
there were two more bytes to go when you checked it.  Another problem is that
with a file of type Text, if you write a string to it that contains the
character $1A (EOF), then the file cannot be read back past that point.

Good luck,

Jeff.

Re:Reading past eof problem


Quote
Jeff Wormsley <daw...@cdc.net> wrote:
>In article <51c6cm$...@imsai.canuck.com>, <lwil...@canuck.com> writes:
>> Why is it that with the following code:

>> assign(f,FileName);      {a file of records}
>> reset(f);
>> while not eof(f)
>>   read(f,Record);

>> I on occassion get an 'error 100:Disk Read Error,? Am I trying to read past
>> the eof? How do I avoid the problem? All help appreciated!
>Likely the file you are reading has less than a complete record at the end.  
>For instance if your records are 10 bytes, and your file has 12 bytes, then you
>will get that error on the second read, but the eof will not be true because
>there were two more bytes to go when you checked it.  Another problem is that
>with a file of type Text, if you write a string to it that contains the
>character $1A (EOF), then the file cannot be read back past that point.
>Good luck,
>Jeff.

Actually, if you do a reset without specifying a record size, or using
a "file of type" reference in the file declaration, you have an
untyped file, whose default record size is 128 bytes (a holdover from
traditional pascal).    If you had a record type of "recrd" you could
define f like so:
var f : file of recrd;

Alternatively, to get single byte granularity, you could leave f
undefined and use the record size field, as in:
 reset(f,1)
 and use blockread/blockwrite commands for i/o.  

Nick

Other Threads