| re: catching EOF errors
"fred" <jcatto@space.qinetiq.com> wrote in message
news:1940ee3f.0405190225.6ad663d1@posting.google.c om...[color=blue]
> Hello,
>
> I read a file from several classes and want to catch an EOF when it
> appears.
> Having caught the EOF I want to allow the final part of the program to
> continue rather than just exiting.
>
> At present I use methods which read from the file; test the read was
> successful and return a 1 if not. This is then checked by the calling
> method, which also returns a 1 if the other method returned 1 (thus
> indicating the EOF) and so on...[/color]
Why not pass a reference to the stream? (Either via return
or a parameter). Then you can query its 'state' bits (e.g. via 'eof()',
'fail()', etc.). That would enable you to distinguish between EOF and a
'real' error.
[color=blue]
>
> As you can see, this is very clumsy and means that I have to
> constantly check the return value. Sometimes this value is almost
> passed back to main() several class levels away![/color]
You can achieve 'direct' communication across several 'levels'
with exceptions.
[color=blue]
>
> Also, where I use unsigned char methods, I do not know what character
> to allocate to the EOF flag to return.[/color]
EOF is by definition a negative value, so storing it in an
unsigned type doesn't make much sense.
Use the "C++ way", and test your stream state.
-Mike |