Thread: why do we test fgets for return of null

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    why do we test fgets for return of null

    This may sound silly, but why do we check for NULL when calling fgets.

    if ( fgets( string_in, BUFSIZ, stdin ) == NULL ) {
    //what goes here
    }

    I couldnt find an answer in any of my text books or on google.

    Also if more characters than BUFSIZ allows is entered into the console, do they remain in the buffer until next time we read from it?

    Thanks in advance.
    "Assumptions are the mother of all **** ups!"

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, fgets probably won't return NULL on stdin - only with files when the EOF condition has been met. the usual file reading loop would be:

    Code:
    while(fgets(buff, max, fp)) {
     // process a line
     }
    for stdin, you might just test that the first character in the buffer is not a newline (meaning the user entered nothing - just depressed 'ENTER'), ie:

    Code:
     while(*fgets(buff, max, stdin) != '\n') {
      // process
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can input EOF to stdin, in which case fgets() will return NULL when there is nothing left to return.

    In Windows/DOS the key combination to input EOF is Ctrl+Z.

    Someone will be along shortly with the nix equivalent.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    nix equivalent is ctrl d

    Also if more characters than BUFSIZ allows is entered into the console, do they remain in the buffer until next time we read from it?
    yes

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

  6. #6
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    Thanks for all your feedback guys, I will just check if I have it straight...

    We should always check return values from interactive input, so with fgets we are checking that the return value does not point to a '\n'?

    NB: Thanks also Thantos
    "Assumptions are the mother of all **** ups!"

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >so with fgets we are checking that the return value does not point to a '\n'?

    No, you are checking the return value of fgets to see whether the call was successful; that is, whether characters have been read into the array without error.
    Last edited by Dave_Sinkula; 07-16-2004 at 08:23 AM. Reason: Added link to fgets man page.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Quote Originally Posted by Kinasz
    This may sound silly, but why do we check for NULL when calling fgets.

    if ( fgets( string_in, BUFSIZ, stdin ) == NULL ) {
    //what goes here
    }

    I couldnt find an answer in any of my text books or on google.
    What does your C-book say about the value returned by fgets()? Note that stdin is redirectable from any stream including a file. Note also that on most systems, you can simulate an EOF on stdin via a peculiar combinaion of keys (ctrl-Z, ctrl-D etc.)
    Also if more characters than BUFSIZ allows is entered into the console, do they remain in the buffer until next time we read from it?
    Assuming the size of the array of char pointed by string_in is >= BUFSIZ, yes, the unread characters are stored somewhere and ready do be read. You are aware of this situation because there is no final '\n' on the currently read string.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM