Search code examples
cblockingfgets

not getting expected output from fgets() function


Here simple code I am not getting the expected output.

#include<stdio.h>

int main()
{
   char buf[1024];
   while(1)
   {
      fgets(buf,strlen(buf),stdin);
      printf("%s",buf);
      printf("hello");
   }
}

In the above code I am want whatever the string I enter from keyboard to be printed out to me the same and then hello. As I know fgets() is a blocking function till I input a string from keyboard and press ENTER it will block the program till that time. So when I run it I expect it as follows

$ ./a.out
I input some text here <ENTER>
I input some text here 
hello

But actually what I am getting output is infinite loop of "hello" printed on terminal. Why my fgets() not blocking the program? Any idea?


Solution

  • That's because the strlen of your buffer is probably zero. You should be using sizeof instead, which gives you the size of the array (1024) rather than the length of the string it contains (indeterminate here).

    It probably is a zero sized string in your particular case (*buf == '\0') since the fgets call is not blocking at all. Actually, it could have a length of one as well since the standard states:

    The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

    In actual fact, as a local variable that hasn't been initialised, buf could contain anything so you're unwise to rely on it (if it doesn't contain a null terminator at all, you may even find yourself dumping core as the strlen runs off the end).

    If you want a tried and true input function, see here. It has buffer overflow protection, prompting, removal of the newline at the end, and clearing of the line remainder in the event it was too long. I'll duplicate the code below to make this answer more self-contained.

    #include <stdio.h>
    #include <string.h>
    
    #define OK       0
    #define NO_INPUT 1
    #define TOO_LONG 2
    static int getLine (char *prmpt, char *buff, size_t sz) {
        int ch, extra;
    
        // Get line with buffer overrun protection.
        if (prmpt != NULL) {
            printf ("%s", prmpt);
            fflush (stdout);
        }
        if (fgets (buff, sz, stdin) == NULL)
            return NO_INPUT;
    
        // If it was too long, there'll be no newline. In that case, we flush
        // to end of line so that excess doesn't affect the next call.
        if (buff[strlen(buff)-1] != '\n') {
            extra = 0;
            while (((ch = getchar()) != '\n') && (ch != EOF))
                extra = 1;
            return (extra == 1) ? TOO_LONG : OK;
        }
    
        // Otherwise remove newline and give string back to caller.
        buff[strlen(buff)-1] = '\0';
        return OK;
    }