fflush(stdin)的对错?
C和C++的标准里从来没有定义过 fflush(stdin)。也许有人会说:“可是我用 fflush(stdin) 解决了这个问题,你怎么能说是错的呢?”的确,某些编译器(如VC6)支持用 fflush(stdin) 来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux 下的 gcc 就不支持),因为标准中根本没有定义 fflush(stdin)。MSDN 文档里也清楚地写着fflush on input stream is an extension to the C standard(fflush 操作输入流是对 C 标准的扩充)。当然,如果你毫不在乎程序的移植性,用 fflush(stdin) 也没什么大问题。以下是 C99 对 fflush 函数的定义:
int fflush(FILE *stream);
如果 stream 指向输出流或者更新流(update stream),并且这个更新流
最近执行的操作不是输入,那么 fflush 函数将把这个流中任何待写数据传送至
宿主环境(host environment)写入文件。否则,它的行为是未定义的。
原文如下:
int fflush(FILE *stream);
If stream points to an output stream or an update stream in which
the most recent operation was not input, the fflush function causes
any unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.
其中,宿主环境可以理解为操作系统或内核等。
由此可知,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的。故而使用 fflush(stdin) 是不正确的,至少是移植性不好的。
(摘自csdn 论坛 http://bbs.csdn.net/topics/330219044)
fflush(stdin)的对错?的更多相关文章
- linux下fflush(stdin)的使用问题
参考自linux下如何清空(刷新)stdin缓冲区 首先,fflush在C/C++/POSIX标准中只定义了处理输出流的行为,对于像stdin这种输入流,这是未定义行为undefined behavi ...
- fflush(stdin)与fflush(stdout)
1.fflush(stdin): 作用:清理标准输入流,把多余的未被保存的数据丢掉.. 如: int main() { int num; char str[10]; cin>>num; c ...
- ACM的fflush(stdin)的问题
在最近的刷题过程中,因为用到了很多字符串的操作,有时需要多次清空缓冲区,所以用了fflush(stdin);的语句,确实很好用,但是发现在OJ上提交后会出现runtime error的问题.当时并没有 ...
- (转)关于fflush(stdin)清空输入缓存流(C/C++)
来源:http://my.oschina.net/deanzhao/blog/79790 1. 为什么 fflush(stdin) 是错的?首先请看以下程序: #include <stdio.h ...
- fflush(stdin)和fflush(stdout)
转自:http://blog.csdn.net/yeyuangen/article/details/6743416 fflush(stdin)即清理标准输入流,把多余的仍未被保存的数据丢掉. fflu ...
- 解决linux下fflush(stdin)无效
void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); }
- 刨根问底系列(2)——stdin、stdout、FILE结构体、缓冲区和fflush的理解
stdin.stdout.FILE结构体.缓冲区和fflush理解 因为之前调试代码时, printf输出的字符串总是被截断了输出(先输出部分, 再输出剩余的), 当时调试了很久, 才知道问题所在, ...
- C 清空输入缓冲区,以及fflush(stdin)的使用误区和解决方法
转载:https://blog.csdn.net/Veniversum/article/details/62048870 对C 语言初学者来说,fflush(stdin)函数被解释为会清空输入缓冲区的 ...
- getchar fflush 的分析笔记
问题描述: 统计从键盘输入的若干个字符中有效字符的个数,以换行符作为输入结束.有效字符是指第一个空格符前面的字符,若输入字符中没有空格符,则有效字符为除了换行符之外的所有字符. 示例代码: #incl ...
随机推荐
- es6 初始化样式加载到head中
Demo1:function loadCss(css) { css.forEach((path)=>{ console.log($('<link/>', { rel: 'styles ...
- 为什么 java wait/notify 必须与 synchronized 一起使用,jvm究竟做了些什么
这个课题提出来的是原先的线程并发解决的思路.目前解决线程并发,可以是lock接口结合condition 并发问题一直以来就是线程必不可少的话题. java 是第一个内置对多线程支持的主流编程语言.在 ...
- 【转】使用Python学习selenium测试工具
出处:https://my.oschina.net/u/1433482/blog/633231?fromerr=vaxqh9bn
- WPF Button TextBox 圆角
<!--圆角button--> <Style TargetType="Button"> <Setter Property="FontSize ...
- Google Gson实现JSON字符串和对象之间相互转换
User实体类 package com.test.json; /** * User 实体类 */ public class User { private String name; private St ...
- C语言基础知识【简介】
C 简介1.关于 CC 语言是为了编写 UNIX 操作系统而被发明的.C 语言是以 B 语言为基础的,B 语言大概是在 1970 年被引进的.C 语言标准是于 1988 年由美国国家标准协会(ANSI ...
- lua元表(简单例子)
Set = {} Set.mt = {}--定义普通的表作为元表,为了避免命名污染直接放在Set内部 function Set.new(t) local set = {} setmetatable(s ...
- Jmeter 04 JMeter 负载与监听
1. 场景设计 2. 场景设置 3. JMeter性能参数配置 4. 测试监听
- mysql 分组加行号
SELECT end AS row_num_v_, case when @v <> _apply.PATIENT_ID then @v := _apply.PATIENT_ID else ...
- 广告 竞价排名 import Levenshtein as Le seqratio_res = Le.seqratio(chk_name_lsit, cmp_)
pip install python-Levenshtein from openpyxl import Workbook import xlrd import time import Levensht ...