Connecting Tech Pros Worldwide Forums | Help | Site Map

catching EOF errors

fred
Guest
 
Posts: n/a
#1: Jul 22 '05
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...

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!

Also, where I use unsigned char methods, I do not know what character
to allocate to the EOF flag to return.

Does anyone know of a better design for this type of EOF catching?

Cheers
Fred

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: catching EOF errors


fred wrote:
[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...
>
> 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!
>
> Also, where I use unsigned char methods, I do not know what character
> to allocate to the EOF flag to return.[/color]

Why not just use int and... well, EOF?
[color=blue]
> Does anyone know of a better design for this type of EOF catching?[/color]

Throw an exception.


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 22 '05

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


Closed Thread