catching EOF errors

F

fred

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
 
R

Rolf Magnus

fred said:
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.
 
M

Mike Wahler

fred said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top