fflush() is ok, when it is used in the proper manner. Read the FAQ on it.Quote:
Originally Posted by mabuhay
Printable View
fflush() is ok, when it is used in the proper manner. Read the FAQ on it.Quote:
Originally Posted by mabuhay
My compiler, VC++ 6.0, doesn't like it. fflush() requires one parameter. :eek:Quote:
Originally Posted by kermit
>My compiler, VC++ 6.0, doesn't like it. fflush() requires one parameter.
This is precisely why I don't add parens to function names when I'm talking about them. ;) To clarify for everyone who doesn't like anything titled "FAQ", fflush ( stdout ) is good, fflush ( stdin ) is bad. fflush is defined as only working on output streams, and passing it an input stream is undefined according to the standard.
I'm just too lazy to write the parameters... ;)
but, does someone had ever problems using fflush(stdin)?
i think it is known, if you use fgets(some_parameters*g*), that you have to empty the stdin if you want to use fgets again. So why is there no c-function to handle this?
>does someone had ever problems using fflush(stdin)?
Go to a non-Windows system and try to use it.
>if you use fgets(some_parameters*g*), that you have to empty the stdin if you want to use fgets again
Only if the line is longer than your buffer, otherwise fgets has no problems. You're probably thinking of using scanf, then the a newline probably has to be extracted before fgets will do what you want.
>So why is there no c-function to handle this?
Because if you did things the right way to begin with, you wouldn't have problems. ;)
Even if fflush(stdin) were somehow "valid", it doesn't even make logical sense. When you flush an output buffer, all data that is buffered will be written out. The logical opposite of this when applied to input buffers is that all data that is buffered will be read in. Guess what does this? Functions that read from the stream (fgets/scanf/fread/getc/etc.).Quote:
Originally Posted by mabuhay
You don't want to discard input, you want to process it and deal with it.
ah - hahaQuote:
Originally Posted by Ancient Dragon
I tend to write my functions that way (with the parentheses) just to indicate I am referring to a function. I never considered that it would be confusing that way though.
Yes, but if I use fgets() I stored my input in a variable but something is still in the input. Or why does it jups over if I use fgets() in the same code again?Quote:
Originally Posted by cwr
I don't want to use scanf(), because you can't define the input-length globally.Quote:
Originally Posted by Prelude
Ok, can someone give me a code example how to use fgets() correctly? If i want to take three lines of text one after other, each with maximal 50 characters.
Really?Quote:
Originally Posted by mabuhay
You can, it's just not particularly convenient.Quote:
I don't want to use scanf(), because you can't define the input-length globally.
Well, I can illustrate an issue with fgets:Quote:
Ok, can someone give me a code example how to use fgets() correctly? If i want to take three lines of text one after other, each with maximal 50 characters.
Output:Code:#include <stdio.h>
#define NLINES 3
int main(void)
{
char buf[NLINES][50];
int i;
for (i = 0; i < NLINES; i++)
{
printf("Enter line %d: ", i);
fflush(stdout);
fgets(buf[i], sizeof buf[i], stdin);
}
for (i = 0; i < NLINES; i++)
printf("Line %d was \"%s\"\n", i, buf[i]);
return 0;
}
The problem being that fgets will stop reading if it hits the size limit. One way to check to see if fgets has read a whole line is to check for the presence of a \n on the end, and discard further input with another fgets (which may overflow again), or a getchar loop, until you reach the actual end of line.Code:Enter line 0: Line number zero
Enter line 1: A really long line one that exceeds fifty characters
Enter line 2: Line 0 was "Line number zero
"
Line 1 was "A really long line one that exceeds fifty characte"
Line 2 was "rs
"
That may not always work either if the input stream does not contain a '\n'. For example the last line of a file may not terminate with '\n', in that case fgets() will not put one at the end of the line either.Quote:
Originally Posted by cwr
True, so you should also check for a NULL return or an feof on the stream.
OK, now i got it! If ther are too much characters in the input buffer, the maximal number of characters will be read and the rest will stay in the stdin and if the next scanf() is called, it will take the remaining characters. Because the end of the remaining characters is a "\n" it will straight continue.
now i understand why cwr said:
i thought, the input that is already processed (copyed in your variable) is still in the stdin.Quote:
...You don't want to discard input, you want to process it and deal with it.
and Prelude said:
when you use scanf to read just one character (scanf("%c", &character); ) then the next scanf() will be jumped over and it seems that in the variable of the second scanf() is nothing, but there's the "\n" in it.Quote:
...You're probably thinking of using scanf...
thx for your help and patience ;)