hi all,
I am a novice of C. i've read almost all articles about the topic of flushing the stdin,
I mimic and try to make my own flush function ( myfflush() ).
then I compare it with fflushall(), fflush(stdin)--not caring the portability.Code:#include "stdio.h" #include "string.h" char str[5]; void main() { //char str[5]; /*why can't here?*/ void myfflush(); fgets ( str, sizeof( str ), stdin ); //flushall(); /* these 3 funcs,only one available each time*/ //myfflush(); //fflush ( stdin ); printf ( "str is:%s\n", str ); printf( "Stdin is:%s\n", stdin ); //fflush ( stdout ); /* usage of ffush(stdout)?*/ printf ( "Stdout is:%s\n", stdout ); } void myfflush() { //extern char str[5]; /* why can't work when defined in main()*/ char eat; if ( !strchr( str, '\n') ) { while ( (eat = getchar() != '\n' ) && (eat != '\0') ) ; } }
when I do this in tc++, it works well, here is the input and outputs:
with any one of the 3 functions: (remove "//" before the function when using it, and add it back when using another)
input: abcdefgh
output: str is:abcd
Stdin is:
Stdout is:
it indicates that all the 3 functions worked well and had the same result.
***********************
when I do this in vc++6.0, the returned results are the same,but they have a little different with that of tc,
Stdin and Stdout have contents this time.
with any one of the 3 functions:
input: abcdefgh
output: str is:abcd
Stdin is:(some confusing characters)
Stdout is:(some other confusing characters)
***********************
then I run it in VC++2005, my function( myfflush() ) seems to work differently with the other two functions,as the
Stdin is different...:
1.with fflush(stdin) or flushall();
input: abcdefgh
output: str is:abcd
Stdin is:(some confusing characters--let's call it cstring1.)
Stdout is:(some other confusing characters--let's call it cstring2.)
2.with myfflush();
input: abcdefgh
output: str is:abcd
Stdin is:(some confusing characters--different with cstring1)
Stdout is:(some other confusing characters--the same with cstring2.)
Here are my questions:
1.Does it mean that my function( myfflush() ) doesn't have the same function with the other two?
if it does, where?
2.what does fflush( stdout ) do as it doesn't clean the screem~...? does printf() reads the string
from str[5] to a buffer of stdout, and fflush( stdout ) clears this buffer? if so, why that buffer
left uncleaned after performing printf();
3.i try to define str[5] inside main() and declare it in myfflush() with extern char str[5], but it
doesn't work , why?
many thanks! :)


, i'll think about it.