Check for EOF when using fgets

This is a discussion on Check for EOF when using fgets within the C Programming forums, part of the General Programming Boards category; hey. Code: #include <stdio.h> FILE *fp; int main (void) { int x; char filename[80], buf[200]; puts("Enter file to review"); scanf("%s", ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    Check for EOF when using fgets

    hey.

    Code:
    #include <stdio.h>
    
    FILE *fp;
    
    int main (void)
    {
      int x;
      char filename[80], buf[200];
    
      puts("Enter file to review");
      scanf("%s", filename);
    
      if ((fp = fopen(filename, "r")) == NULL)
      {
        puts("Error1");
        return(-1);
      }
    
      while(1)
      {
        if (fgets(buf, 200, fp) == NULL)
        {
          puts("Error2 or EOF");
          return(-1);
        }
       printf("%s", buf);
      }
      
      return 0;
    }
    anyone who can tell me about a way of checking for EOF within this while loop. I can't think of a good sollution. I would like to print an error if something else than EOF is found by fgets.

    dagH

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Something more like this:

    Code:
    while(fgets(buf, 200, fp) != NULL)
    {
          printf("%s",buf);
    }
    
    /* fgets() returned NULL.  Now you can check why.... */
    
    if(feof(fp)
    {
          /* EOF */
    }
    else if(ferror(fp))
    {
          /* Errror */
    }
    ...

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Hey.
    That was what I was looking for. I haven't used ferror before so...
    But is there a way to do the same thing without ferror?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    hmm... but why do you want to avoid ferror()?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    well for the simple reason that I thought I had seen an example not using ferror() and was just wondering if there was something I was missing...

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    In this example that I posted, ferror() just checks if there was an error that caused the fgets() to fail. If you're doing error checking, why wouldn't you want to use it?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It's not that hard to use. You just pass it a file pointer, and it returns non-zero if the file stream is in an error state. http://cppreference.com/stdio/ferror.html

    Then you can call perror() to print what the actual error was. Just pass perror() a string to print before the error. http://cppreference.com/stdio/perror.html

    They're both ANSI standard functions. There's nothing wrong with using them unless you have a specific reason not to.
    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. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  3. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 04:13 PM
  4. fgets reads byherself data
    By Phoenix_Rebirth in forum C Programming
    Replies: 5
    Last Post: 12-30-2005, 03:01 PM
  5. prog !feof probs
    By @licomb in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2001, 06:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21