Re: how to check the scanf function if it will read more than one number



Moosdau wrote
(in article
<1136266436.129348.14670@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>):

> I think I've already known the answer.
> the below is I copied from MSDN:

They should be ashamed.

> 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.

Do they bother to point out that it is a non-standard extension
to have this behavior?

No?

Why not?

They want you to write non-portable code, it lends itself to
supporting their goal of monopoly.

> #include <stdio.h>
> #include <conio.h>
>
> int main( void )
> {
> 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 );
> }

This code is quite broken, for several reasons. Do you know
what they are?

The fact that MSDN includes bad code examples is hardly
surprising, considering the source (no pun intended).

> then I know,I shouldn't use fflush(stdin) except in VC.
> but in VC, it is safe.

Nothing about the above program is safe. Microsoft *claims*
that they are teaching their own programmers not to code this
way, yet it their examples still contain code which they are
*supposedly* not able to use themselves anymore. Think about
that before blinding following MSDN examples.


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





.



Relevant Pages

  • Re: I need help please!
    ... The fflush function flushes a stream. ... fflush clears the contents of the buffer. ... process input buffer: that data has "arrived" but if it hasn't reached ...
    (comp.lang.c)
  • Re: I need help please!
    ... function that will consume ALL the characters left in the standard input ... The fflush function flushes a stream. ... fflush clears the contents of the buffer. ...
    (comp.lang.c)
  • Re: how to check the scanf function if it will read more than one number
    ... It would do so as an extension. ... > The fflush function flushes a stream. ... > the buffer associated with the stream. ...
    (comp.lang.c)
  • Re: scanf() quesion?
    ... int main ... The fflush function flushes a stream. ... fflush clears the contents of the buffer. ...
    (comp.lang.c)
  • Re: need to convert a char to an hexadecmial value
    ... , to a newline character. ... Here's what the standard says: ... A text stream is an ordered sequence of characters composed into ... fflush() will cause a partial line to be written to the output file ...
    (comp.lang.c)