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)的对错?的更多相关文章

  1. linux下fflush(stdin)的使用问题

    参考自linux下如何清空(刷新)stdin缓冲区 首先,fflush在C/C++/POSIX标准中只定义了处理输出流的行为,对于像stdin这种输入流,这是未定义行为undefined behavi ...

  2. fflush(stdin)与fflush(stdout)

    1.fflush(stdin): 作用:清理标准输入流,把多余的未被保存的数据丢掉.. 如: int main() { int num; char str[10]; cin>>num; c ...

  3. ACM的fflush(stdin)的问题

    在最近的刷题过程中,因为用到了很多字符串的操作,有时需要多次清空缓冲区,所以用了fflush(stdin);的语句,确实很好用,但是发现在OJ上提交后会出现runtime error的问题.当时并没有 ...

  4. (转)关于fflush(stdin)清空输入缓存流(C/C++)

    来源:http://my.oschina.net/deanzhao/blog/79790 1. 为什么 fflush(stdin) 是错的?首先请看以下程序: #include <stdio.h ...

  5. fflush(stdin)和fflush(stdout)

    转自:http://blog.csdn.net/yeyuangen/article/details/6743416 fflush(stdin)即清理标准输入流,把多余的仍未被保存的数据丢掉. fflu ...

  6. 解决linux下fflush(stdin)无效

    void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); }

  7. 刨根问底系列(2)——stdin、stdout、FILE结构体、缓冲区和fflush的理解

    stdin.stdout.FILE结构体.缓冲区和fflush理解 因为之前调试代码时, printf输出的字符串总是被截断了输出(先输出部分, 再输出剩余的), 当时调试了很久, 才知道问题所在, ...

  8. C 清空输入缓冲区,以及fflush(stdin)的使用误区和解决方法

    转载:https://blog.csdn.net/Veniversum/article/details/62048870 对C 语言初学者来说,fflush(stdin)函数被解释为会清空输入缓冲区的 ...

  9. getchar fflush 的分析笔记

    问题描述: 统计从键盘输入的若干个字符中有效字符的个数,以换行符作为输入结束.有效字符是指第一个空格符前面的字符,若输入字符中没有空格符,则有效字符为除了换行符之外的所有字符. 示例代码: #incl ...

随机推荐

  1. u-boot README--linux support

    Linux HOWTO:============ Porting Linux to U-Boot based systems:------------------------------------- ...

  2. org.hibernate.type.SerializationException: could not deserialize 反序列化失败

    1.查看实体类有没有实现Serializable接口 例:public class Student implements Serializable { ***** } 2.看表中的字段有没有在实体中进 ...

  3. Python 类方法、实例方法、静态方法

    实例方法:类中第一个参数为self的方法. 类方法:类中第一个参数为类,约定写为cls,并被@classmethod修饰的方法. 静态方法:类中被@staticmethod修饰的方法. 类变量:定义在 ...

  4. Downloading jQuery

    Compressed and uncompressed copies of jQuery files are available. The uncompressed file is best used ...

  5. No breeds found in the signature, a signature update is recommended

    cobbler 2.6.11 遇到这个问题,需要 >> cobbler signature update >> and cobblerd restart 转自: https:/ ...

  6. React Native安装步骤

    先贴出中文网安装指南:http://reactnative.cn/docs/0.46/getting-started.html 本文会点出一些安装时遇到的坑,和解决方案! 1.首先是安装Chocola ...

  7. 2820: YY的GCD

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1693  Solved: 901[Submit][Status][Discu ...

  8. poj2816

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29799   Accepted: 12090 De ...

  9. 大数据学习系列(1)-- linux之文件系统结构介绍

    1./ 根目录 --------- 所有目录挂在其下 2./boot --------- 存放Ubuntu内核和系统启动文件.系统启动时这些文件先被装载. 3./etc --------- 系统的配置 ...

  10. twig 截取字符串

    <p>{{content|slice(0,100)}}</p> slice()截取content变量值,从0到100