Thread: How do you catch a read error from stdin?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Question How do you catch a read error from stdin?

    Hi guys!

    My program basically reads from a text file or whatever is piped into it through std in and I need to catch a read error, but I'm confused on what to even look for exactly.

    Someone would run my program like this:
    myProgram < aTextFile.txt

    Here is some of my code what does the reading:
    Code:
     while ((c = getc(stdin)) != EOF)
     76     {
     77       /*counting characters*/
     78       characters++;
     79 
     80       /*counting special characters*/
     81       if(isprint(c) != 0)
     82         printable++;
     83       if(iscntrl(c) != 0)
     84       {
     85         control++;
     86       }
     87       if(c == '\n')
     88        newline++;
    As you can see i'm just using getc to read each character, but what would I check if there was an error reading the file? if an error did occur I would just exit(1) but what to check for is where I'm lost.

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If there's a read error, fgetc will return EOF. (Note: typing man fgetc would have gotten you the same information.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Thanks for the response but I'm using getc, not fgetc, unless that was just a typo.
    After looking it up, it seems getc and fgetc are the same so I guess it doesn't matter.

    And I did see that it will return EOF if there was an error like you said, but how would I be able to tell if in fact its an End of File or its actually a read error so I can print an error message?


    I was thinking about reading the next character after EOF, and if EOF does have another character after it, it is infact a read error but who knows what that next character will be if a read error occurs, so the check could be not the best.
    Last edited by mr_coffee; 09-02-2008 at 12:12 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Keep reading:
    Quote Originally Posted by man getc
    If successful, these routines return the next requested object from the
    stream. Character values are returned as an unsigned char converted to
    an int. If the stream is at end-of-file or a read error occurs, the rou-
    tines return EOF. The routines feof(3) and ferror(3) must be used to
    distinguish between end-of-file and error. If an error occurs, the
    global variable errno is set to indicate the error. The end-of-file con-
    dition is remembered, even on a terminal, and all subsequent attempts to
    read will return EOF until the condition is cleared with clearerr(3).
    Naturally, feof returns true if eof happened, and ferror returns true if error happened.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst I'm sure it is POSSIBLE to get an error other than "nothing more to read [technically not an error]", but so far I have never seen it happen to stdin.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Thanks guys!
    I ended up using code like this:

    Code:
    if(ferror(stdin))
      printf("bad things to say");
       exit(1);

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note: if you want to give the user a second chance, have a look at clearerr().

    BTW: fgetc() and getc() are almost the same, but not quite. getc() is implemented as a macro, so it might be slightly faster than fgetc() (and may generate larger code). The same goes for fputc() and putc(), although with putc() you have to be careful not to pass arguments that have side affects, like *c++.

    [edit] Why not use stderr for your error message? Oh, and make sure you print a newline after your message. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by mr_coffee View Post
    Thanks guys!
    I ended up using code like this:

    Code:
    if(ferror(stdin))
      printf("bad things to say");
       exit(1);
    That exit()s unconditionally.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Good catch! Yes, it does indeed. Don't forget to put curly braces around if statements that have more than one statement.

    I can't believe I missed that. But anyway, I doubt that's the actual code. The indenting in the original strikes me as better than that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Determining if stdin stream holds data
    By Stack Overflow in forum C Programming
    Replies: 3
    Last Post: 01-28-2005, 02:52 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM