Windows下和Linux下的fflush的区别
如下程序: (MSDN的fflush函数的例子)#include <stdio.h>
void 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 );
printf( "%s\n", string );
}
/* You must flush the input buffer before using gets. */
fflush( stdin );
printf( "Enter the same sentence with gets: " );
gets( string );
printf( "%s\n", string );
}
上述程序在Windows下运行得到预期结果:
Enter a sentence of four words with scanf: This is a test(回车)
This
is
a
test
Enter the same sentence with gets: This is a test(回车)
This is a test
但是在Linux(Ubuntu 12)下第一个回车后程序正确输出4个单词,但然后显示"Enter the same sentence with gets:" 后便直接退出:
Enter a sentence of four words with scanf: This is a test
This
is
a
test
Enter the sane sentence with gets:
ubuntu@ubuntu-VM:~/Desktop/workspace/strean$
谁能告诉我,为什么Linux下的fflush不起作用呢?