fflush(stdin)的替代方法? [英] Alternative for fflush(stdin)?

查看:961
本文介绍了fflush(stdin)的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fflush(stdin)是C ++中真正需要的,用这种方法刷新缓冲区中的换行符很好吗?

Is fflush(stdin) is really required in C++ and is it good to do it this way to flush the newline in the buffer?

推荐答案

调用 fflush(stdin)是C语言中未定义的行为(以及C ++语言)。

The call to fflush(stdin) is undefined behavior in C (and, consequetly, in C++).

C语言标准ISO 9899:1999在 7.19.5.2/2

The C language standard, ISO 9899:1999 states in 7.19.5.2/2


流指向其中未输入最近的
操作的输出流或更新流,则fflush函数使得流
的任何未写入的数据被递送到主机环境以被写入文件;否则,行为是
undefined

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

要丢弃整个输入直到行的下一行,在C ++中,最稳健的方法是

To discard the entire input up to the next end of line, in C++, the most robust approach is

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

至于程序闪烁而走,你可能在Windows上执行控制台应用程序平台?这样的应用程序最好从控制台窗口(Start-> run-> cmd.exe)

As for the program "flashing and going away", are you perhaps executing a console application on a Windows platform? Such applications are best executed from a console window (Start->run->cmd.exe)

这篇关于fflush(stdin)的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆