By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

catching EOF errors

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
fred wrote:
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.
Why not just use int and... well, EOF?
Does anyone know of a better design for this type of EOF catching?


Throw an exception.
Jul 22 '05 #2

P: n/a
"fred" <jc****@space.qinetiq.com> wrote in message
news:19**************************@posting.google.c om...
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...
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.

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!
You can achieve 'direct' communication across several 'levels'
with exceptions.

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


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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.