C语言第十六篇:fflush函数有什么作用?
2016-05-20 21:57
1031 查看
出处:http://blog.csdn.net/stpeace/article/details/9063293
作者:stpeace的专栏
先来复习一个简单单词吧:
flush(注意只有一个f):冲洗,冲刷,冲掉。 例句:I flushed the toilet and went back to work again.
下面,我们来看看一个简单的函数:fflush(file flush,注意有两个f), 先来看一个简单的程序:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
运行这个程序,输入1, 并按enter键,结果为:
49
10
不用吃惊,这个结果很正常的,字符1对应的ASCII值刚好为49, enter键对应的ASCII值为10, 所以就有这样的结果呢。可以看出,第二个scanf函数执行了,并从缓冲区中得到了值(其实,这个值不是我们想要的),那么我们如何把缓冲区这个“马桶”里面的值冲掉呢?用fflush函数就可以了。如下:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
fflush(stdin); // 冲掉“马桶”中的无用值
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
这样,就不会显示10了。
下面,我们来看MSDN(2008)的一个例子(MSDN上给的程序当然是对的啊):
[cpp] view
plain copy
#include <stdio.h>
#include <conio.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 );
}
要是不信那个邪,你把上面程序中的fflush那一行注释掉,运行一下程序,你就知道有什么后果了。从而,你也就懂了fflush的作用。
最后,我们看看MSDN中一段话,以此结束本文:
fflush has no effect on an unbuffered stream.
Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream.
作者:stpeace的专栏
先来复习一个简单单词吧:
flush(注意只有一个f):冲洗,冲刷,冲掉。 例句:I flushed the toilet and went back to work again.
下面,我们来看看一个简单的函数:fflush(file flush,注意有两个f), 先来看一个简单的程序:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
运行这个程序,输入1, 并按enter键,结果为:
49
10
不用吃惊,这个结果很正常的,字符1对应的ASCII值刚好为49, enter键对应的ASCII值为10, 所以就有这样的结果呢。可以看出,第二个scanf函数执行了,并从缓冲区中得到了值(其实,这个值不是我们想要的),那么我们如何把缓冲区这个“马桶”里面的值冲掉呢?用fflush函数就可以了。如下:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
fflush(stdin); // 冲掉“马桶”中的无用值
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
这样,就不会显示10了。
下面,我们来看MSDN(2008)的一个例子(MSDN上给的程序当然是对的啊):
[cpp] view
plain copy
#include <stdio.h>
#include <conio.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 );
}
要是不信那个邪,你把上面程序中的fflush那一行注释掉,运行一下程序,你就知道有什么后果了。从而,你也就懂了fflush的作用。
最后,我们看看MSDN中一段话,以此结束本文:
fflush has no effect on an unbuffered stream.
Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream.
相关文章推荐
- Linux C函数参考手册(PDF版)
- C# partial关键字说明
- Lua教程(十七):C API简介
- 简单谈谈lua和c的交互
- C#中的委托数据类型简介
- C#编写的艺术字类实例代码
- C#实现打造气泡屏幕保护效果
- 举例讲解C#编程中委托的实例化使用
- 使用C#代码获取存储过程返回值
- C++中explict关键字用法
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- 基于C语言fflush()函数的使用详解
- C 语言基础教程(我的C之旅开始了)[三]
- 学习C和C++的9点经验总结
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析