fgets returns null

This is a discussion on fgets returns null within the C Programming forums, part of the General Programming Boards category; I have written the following function to get user input from stdin and clear the buffer. Code: char* getUserInput() { ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 11: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-08-2004, 12:51 AM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 10:57 AM

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