fflush函数有什么作用?
说明:
有的朋友对本文的程序结果提出质疑,所以这里说一下,我是在Windows VC++6.0上测试的, 请注意平台和环境的不同。
先来复习一个简单单词吧:
flush(注意只有一个f):冲洗,冲刷,冲掉。
例句:I flushed the toilet and went back to work again.
下面,我们来看看一个简单的函数:fflush(file flush,注意有两个f), 先来看一个简单的程序:
#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函数就可以了。如下:
#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上给的程序当然是对的啊):
#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.
fflush函数有什么作用?相关推荐
- fflush函数作用浅析
一.fflush函数:更新缓存区 头文件:#include <stdio.h> 函数定义:int fflush(FILE *stream); 函数说明:fflush()会强迫将缓冲区内的数 ...
- fwrite函数与fflush函数
以下内容源于网络资源的学习与整理,欢迎交流. 一.fwrite函数 1.函数原型 size_t fwrite(const void *ptr, size_t size, size_t nmemb,FI ...
- linux中fflush函数和printf函数 【转】
本文转载自:http://blog.chinaunix.net/uid-30058258-id-5029847.html printf是一个行缓冲函数 printf函数是标准函数,最终会调用到系统调用 ...
- linux系统fflush函数
在linux系统里面,一般都是行刷新,也就是要输出的内容会先放在缓冲区里面,直到遇到换行符,才会将缓冲区里的内容全部输出到屏幕或者文件中. 函数原型 #include <stdio.h> ...
- sync、fsync、fdatasync、fflush函数区别和使用举例
sync.fsync.fdatasync.fflush函数区别和使用举例 Linux/unix在内核中设有缓冲区.高速缓冲或页面高速缓冲,大多数磁盘I/O(block device)都通过缓冲进行,当 ...
- php页面最大执行时间 set_time_limit函数不起作用
作者: default|标签:PHP set_time_limit 执行时间|2017-3-21 15:03 set_time_limit 不生效或者无效解决方法 <?php global $b ...
- php curl_init不能用,curl_init()函数不起作用
curl_init()函数不起作用 嗨,我在POST请求中尝试PHP Post Request,认为它可能对我有用,我的代码如下所示$sub_req_url = "http://localh ...
- linux fflush函数,fflush - wwliu的个人页面 - OSCHINA - 中文开源技术交流社区
概述 函数名: fflush 功 能: 清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件 头文件: #include 原型:int fflush(FILE *stream) #include # ...
- matlab vec2ind,MATLAB函数ind2vec的作用
MATLAB函数ind2vec的作用 mip版 关注:143 答案:3 悬赏:10 解决时间 2021-01-25 07:24 已解决 2021-01-24 21:30 >> p=[ ...
最新文章
- Asp.Net 之 抓取网页内容
- 条理清晰的搭建SSH环境之添加所需jar包
- uva437巴比伦塔
- 音视频技术的高光时刻: LiveVideoStackCon 2019上海 音视频技术大会
- centos 6.7 安装 最新版 git
- 【转】Serverless架构
- ubuntu 下的文件搜索
- StringBuilder常用函数
- python生成wifi字典_python生成密码字典的方法
- 学习笔记:SSH端口转发的三种方式
- 如何发布ArcGIS Server离线地图(google 瓦片)
- 比特率和波特率的公式解析
- 10.解决分支合并冲突
- 坚持努力,在黑暗中寻找光明——我的2014
- 综述论文要写英文摘要吗_论文写作指导:本科学位毕业论文写作必备技巧
- MySQL 幻读被彻底解决了吗?
- 多余元素删除(多种代码)
- java 自定义控件_自定义控件的开发
- Windows进行Java Web开发,电脑上安装什么?
- 电锤、冲击钻和空心钻的使用方法