fgets always returns NULL on stdin
Posted: Mon Mar 20, 2017 12:36 pm
I'm trying to use fgets to read from a terminal on stdin. Rather than waiting for a return, a full buffer or an EOF it always returns NULL.
When I type a characters in it gets it - sometimes more than one character. Is there a timeout somewhere that I'm not aware of?
Code looks like this n(i.e. it always returns NO_INPUT)
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 (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
buff[strlen(buff)-1] = '\0';
return OK;
}
When I type a characters in it gets it - sometimes more than one character. Is there a timeout somewhere that I'm not aware of?
Code looks like this n(i.e. it always returns NO_INPUT)
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 (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
buff[strlen(buff)-1] = '\0';
return OK;
}