zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    React Native商城项目实战08
    React Native商城项目实战07
    React Native商城项目实战05
    React Native商城项目实战06
    React Native商城项目实战04
    React Native商城项目实战03
    React Native商城项目实战02
    单选框input:radio
    myDate97用法
    STRUTS2配置动态页面
  • 原文地址:https://www.cnblogs.com/gongpixin/p/4477432.html
Copyright © 2011-2022 走看看