Reputation: 45
I was writing code which called 2 different functions which us fgets(), but I noticed the second fgets() was skipped over. I tried making a test file to fix this,but I noticed even when writing stupidly simple code it still won't wait for me to type the second time around. EDIT: changed the [3] into a [4], still doesn't work though.
#include <stdio.h>
main()
{
char I[4];
fgets(I,3,stdin);
printf("%s",I);
fgets(I,3,stdin);
printf("%s",I);
}
Upvotes: 1
Views: 1116
Reputation: 420
The problem occurs because the keyboard buffer is not cleared. Basically in the first fgets
if the keyboard input reaches the limit of the input buffer size, the rest will be read by the next fgets
.
This example works according to what you went through with the problem:
#include <stdio.h>
#include <string.h>
void flushBuffer() {
int ch;
while (((ch = getchar()) != EOF) && (ch != '\n'));
}
int main()
{
char I[3];
fgets(I, sizeof(I), stdin);
printf("\n%s",I);
if ( strchr( I, '\n' ) == NULL )
{
flushBuffer();
}
fgets(I, sizeof(I), stdin);
printf("\n%s",I);
return 0;
}
Another point is to use the sizeof
operator to indicate the size of the vector.
As for the size of the vector, it really is quite small, if possible put a larger value according to your need.
Upvotes: 1
Reputation: 1702
The prototype of fgets()
defines it as, char *fgets(char *str, int n, FILE *stream)
,with n - 1
being the maximum number of characters to be read, for an input like:
A1\n
, you have three characters and the NULL byte, so A1\n\0
.
By setting n
to 3, you tell fgets()
that it will read 2 characters at most plus \0
to terminate the string. Therefore, the newline (\n
) character is left in the buffer, later to be consumed by the next fgets()
call.
So changing fgets(I, 3, stdin)
to fgets(I, 4, stdin)
should fix your problem. You might also want to consider checking the return value of fgets()
in case it returns a NULL pointer.
Upvotes: 1
Reputation: 154198
I thought that it would need just 3
To fully read in "A1\n"
, fgets()
needs at least 4. 3 for the line1 read and 1 for the appended null character to form a string.
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. C2x dr § 7.21.7.2 2
With input "A1\n"
and fgets(I,3,stdin);
, fgets()
reads the "A1"
, leaving the '"\n"
for the next fgets()
. That 2nd fgets()
returns promptly as it read a '\n'
.
Instead
Use a generous buffer. I recommend 2x whatever you think the max sane input will be.
Pass into fgets()
the size of the buffer.
Check return
Print with sentinels for clarity
.
// char I[4];
// fgets(I,3,stdin);
// printf("%s",I);
char I[100];
if (fgets(I, sizeof I, stdin)) {
printf("<%s>\n", I);
}
1 In C, "each line consisting of zero or more characters plus a terminating new-line character."
Upvotes: 1