catching EOF errors

Discussion in 'C++' started by fred, May 19, 2004.

  1. fred

    fred Guest

    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
    fred, May 19, 2004
    #1
    1. Advertising

  2. fred

    Rolf Magnus Guest

    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.
    Rolf Magnus, May 19, 2004
    #2
    1. Advertising

  3. fred

    Mike Wahler Guest

    "fred" <> wrote in message
    news:...
    > 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
    Mike Wahler, May 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    913
    Mark Goldin
    Jan 17, 2004
  2. Kobu
    Replies:
    10
    Views:
    600
    Keith Thompson
    Mar 4, 2005
  3. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    667
    James Kanze
    Jun 15, 2007
  4. yawnmoth
    Replies:
    97
    Views:
    4,626
    Bent C Dalager
    Feb 27, 2009
  5. Jan Burse
    Replies:
    67
    Views:
    1,022
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page