E
pemo said:Keith Thompson said:What if getchar() returns EOF before returning '\n'?pemo said:news:[email protected]... [...]
We do. My variant of Richards function is:
int flushln(FILE *f)
{
int ch;
do {
ch = getc(f);
} while (('\n' != ch) && (EOF != ch));
return ch;
}
Would this be acceptable for stdin?
void flushstdin(void)
{
while(getchar() != '\n')
;
return;
}
Surely, if there's a \n in the buffer, getchar won't return EOF?
If there's not a \n in the buffer, I can see that testing for EOF might be a
good idea.
pemo said:Surely, if there's a \n in the buffer, getchar won't return EOF?
If there's not a \n in the buffer, I can see that testing for EOF might be a
good idea.
Emmanuel Delahaye said:pemo a écrit :
No, because stdin can be redirected from a file. EOF must be checked too.
Note that the 'return;' is useless.
pemo said:.... snip ...
*would* make sense [surely?] to have some way of removing any
unread characters held in the stdin stream?
We do. My variant of Richards function is:
int flushln(FILE *f)
{
int ch;
do {
ch = getc(f);
} while (('\n' != ch) && (EOF != ch));
return ch;
}
Would this be acceptable for stdin?
void flushstdin(void)
{
while(getchar() != '\n')
;
return;
}
True. However, lacking the latter, use of the former requires thatKeith said:.... snip ...
Note that there are two different things being discussed here.
One, which is easily implementable in standard C, is discarding
all the remaining characters in a line. The other, which can't
be done in standard C, is discarding all typed characters that
haven't been processed yet, i.e., flushing the typeahead buffer.
I think the former is what the OP really wants.
Richard Heathfield said:Emmanuel Delahaye said:
Only to the compiler.
Richard said:For the same reason that pressing the handle on your toilet will not get rid
of the water waiting in your sink's tap (or faucet, if you're on that side
of the pond). Flushing is something we do to output, not to input.
Moosdau said:thanks.
but what I want to know is the "actual" reason,
not a "logical" one.
e.g.
void f( int*p )
{
if (p->num)
do something;
}
when this function is running, there will be an error.
if I don't know the reason,what I want to know is :
if p is a NULL pointer, p->num will cause the error.
so ,the fflush function works very well on my computer now,
If it cause a potential danger,
I want to know what is it, and what is the condition.
if possible, an example is the best.
it do be that.Peter said:Your implementation probably defines a behaviour fflush on
input streams. It would do so as an extension. That extension
is not topical in comp.lang.c.
Moosdau said:it do be that.Peter said:Your implementation probably defines a behaviour fflush on
input streams. It would do so as an extension. That extension
is not topical in comp.lang.c.
I think I've already known the answer.
the below is I copied from MSDN:
The fflush function flushes a stream. If the file associated with
stream is open for output, fflush writes to that file the contents of
the buffer associated with the stream.
If the stream is open for input, fflush clears the contents of the
buffer.
Example
// crt_fflush.c
#include <stdio.h>
#include <conio.h>
int main( void )
{ [snip]
int integer;
char string[81];
/* Read each word as a string. */
printf( "Enter a sentence of four words with scanf: " );
for( integer = 0; integer < 4; integer++ )
{
scanf( "%s", string );
// Security caution!
// Beware allowing user to enter data directly into a buffer
// without checking for buffer overrun possiblity.
printf( "%s\n", string );
}
/* You must flush the input buffer before using gets. */
fflush( stdin ); // fflush on input stream is an extension to the
C standard
printf( "Enter the same sentence with gets: " );
gets( string );
printf( "%s\n", string );
}
then I know,I shouldn't use fflush(stdin) except in VC.
but in VC, it is safe.
Moosdau said:it do be that. I think I've already known the answer. the below
is I copied from MSDN:
The fflush function flushes a stream. If the file associated
with stream is open for output, fflush writes to that file the
contents of the buffer associated with the stream. If the stream
is open for input, fflush clears the contents of the buffer.
Implying that it's useful to the reader? This was a "return;" at the
very end of a void function; how is that useful at all?
thanks.
but what I want to know is the "actual" reason,
not a "logical" one.
if p is a NULL pointer, p->num will cause the error.
so ,the fflush function works very well on my computer now,
If it cause a potential danger,
I want to know what is it, and what is the condition.
if possible, an example is the best.
Richard said:Okay, here's an example. Let's say you choose to use fflush(stdin) because
Microsoft say it's okay, and then in five years time Microsoft goes bust
and everyone starts using Macs instead, and you port your code to a Mac and
suddenly it stops working, and the reason it stops working is that you
thought "MSDN" was an acceptable substitute for "The ISO C Standard" and
wrote your code on that basis. Well, if you want to create unnecessary
portability headaches for yourself, that's your lookout.
The irony is that this is such a pointless discussion, since you simply
don't /need/ to "flush" input. Well, I don't, anyway, and I do a /lot/ of
text processing in C.
>
> thanks.
> but what I want to know is the "actual" reason,
> not a "logical" one.
Keith Thompson said:So perhaps you should do something like this:
void flushstdin(void)
{
if (theres_a_newline_in_the_buffer()) {
while(getchar() != '\n')
;
}
else {
int c;
while(c = getchar(), c != '\n' && c != EOF)
;
}
}
Implementing the theres_a_newline_in_the_buffer() function in portable
C is left as an exercise. If you can't think of a way to do it,
perhaps you should just test for EOF.
To answer my own question from upthread, if you reach EOF on stdin
before seeing a '\n' character, your flushstdin() function becomes an
infinite loop; getchar() will repeatedly return EOF, and the condition
will never allow the loop to terminate.
Richard Heathfield said:Keith Thompson said:
It's a tiny, tiny advantage, but an advantage nonetheless, at least to me.
My functions generally have one entry point and one exit point. The exit
point is the return statement. So I'm accustomed to writing one in each
function, generally as the /last/ line typed in the first cut of the
function. (The closing brace is typed immediately after the opening brace,
to save me from encountering pairing-up nonsenses later.)
So, when I see a function with no return statement, I find myself thinking
"is this function finished yet?"
Emmanuel Delahaye said:
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.