Thread: fgets returns null

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    fgets returns null

    I have written the following function to get user input from stdin and clear the buffer.

    Code:
    char* getUserInput() {
    
       static char buffer[BUFFER_SIZE + 1];
       
       if (fgets(buffer, sizeof buffer, stdin) != NULL) {
    
          if ((strlen(buffer)) == BUFFER_SIZE && 
                buffer[BUFFER_SIZE - 1] != '\n')
             readRestOfLine();
       }
       return buffer;
    }
    *readRestLine is a function provided by my lecturer to clear the buffer.

    This code is working perfectly for me but I am wondering if I need to (or should) have an else statement for when fgets returns null.
    Is it possible for fgets to return null when reading from stdin? If not why is the test for null required?

    I new to C as you can tell and I am just trying to get my head around what is happening here and learn the best practice for these situations

    thanks in advance

    Michael

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by strider1974 View Post
    Is it possible for fgets to return null when reading from stdin? If not why is the test for null required?
    Yes, if the user sends a terminal EOF character (Ctrl-Z on Windows, Ctrl-D on most other things)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    For best practice should I have an else statement to cover fgets returns null?

    Currently the program, using the code above, returns invalid input when ctrl-d is selected which works with the program as written

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by strider1974 View Post
    For best practice should I have an else statement to cover fgets returns null?
    Should you check for errors? Uh... yes?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    Yes, if the user sends a terminal EOF character (Ctrl-Z on Windows, Ctrl-D on most other things)
    Also it is very simple to redirect stdin to read from file (using < command of command line)
    In this case fgets will return null after the last line of the file is read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. fgets erroneoulsy returns null string
    By leonv in forum C Programming
    Replies: 3
    Last Post: 12-04-2006, 06:20 AM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. Problem with a menu.
    By Rare177 in forum Windows Programming
    Replies: 4
    Last Post: 09-07-2004, 11:51 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM