buggy_double_use_of _scanf
hello,
I have observe a strange scanf behaviour...
when using two successive scanf... the second receive a return character
for example :
int a;
char b;
scanf ("%d", &a);
fflush (stdin);
scanf ("%c", &b);
fprintf (stdout, "a : %d\nb : %c\n", a, b);
what's the problem ?
is scanf using a buggy internal buffer ?
simon
Quoted 8 lines · simon
I have observe a strange scanf behaviour...
when using two successive scanf... the second receive a return characterfor example :
int a;
char b;
Quoted 6 lines
scanf ("%d", &a);
fflush (stdin);
scanf ("%c", &b);
fprintf (stdout, "a : %d\nb : %c\n", a, b);what's the problem ?
What makes you think that there is a problem?
What's the input? If it's a decimal number followed by newline, the first scanf() will return the parsed number, the second will return the newline.
Signature · 1 line
Glynn Clements <[email protected]>
----- Original Message -----
From: "Glynn Clements" <[email protected]>
To: "simon" <[email protected]>
Cc: "linux-C-programming" <[email protected]>
Sent: Tuesday, October 26, 2004 6:59 PM
Subject: Re: buggy_double_use_of _scanf
Quoted 16 lines
Quoted 13 lines · simon
I have observe a strange scanf behaviour...
when using two successive scanf... the second receive a return characterfor example :
int a;
char b;scanf ("%d", &a);
fflush (stdin);
scanf ("%c", &b);
fprintf (stdout, "a : %d\nb : %c\n", a, b);
Quoted 7 lines
Quoted 1 line
what's the problem ?
What makes you think that there is a problem?
What's the input? If it's a decimal number followed by newline, the first
scanf()will return the parsed number, the second will return the newline.
The problem, I beleve is in the fflush function. I Have the same behaviour with this source code,
but when I put a additional scanf in the line, all works fine -;)
Now I ask:
Why the fflush function did not remove the new line caracter when it should ?
it´s all folks !!!
Russo.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 22 lines · Fábio Russo
Quoted 13 lines
Quoted 13 lines
I have observe a strange scanf behaviour...
when using two successive scanf... the second receive a return characterfor example :
int a;
char b;scanf ("%d", &a);
fflush (stdin);
scanf ("%c", &b);
fprintf (stdout, "a : %d\nb : %c\n", a, b);Quoted 7 lines
Quoted 1 line
what's the problem ?
What makes you think that there is a problem?
What's the input? If it's a decimal number followed by newline, the first
scanf()will return the parsed number, the second will return the newline.
Quoted 7 lines
The problem, I beleve is in the fflush function. I Have the same behaviour with this source code,
but when I put a additional scanf in the line, all works fine -;)Now I ask:
Why the fflush function did not remove the new line caracter when it should ?
fflush() should not have removed any characters from the buffer. The purpose of fflush() is to ensure that any data in *output* buffers is written to the underlying descriptor.
If your input consists of lines, you should probably be using fgets() to read each line then using sscanf() to parse the text, rather than using scanf() (or fscanf()) directly.
For a start, using fgets() makes it much easier to recover from errors. If you call scanf(), and it indicates that it didn't read everything which you wanted it to, it can be unclear as to exactly what it did read and what is likely to be left in the buffer.
Signature · 5 lines
Glynn Clements <[email protected]> - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html