cin.clear()将EOF留在流中? [英] cin.clear() leave EOF in stream?

查看:87
本文介绍了cin.clear()将EOF留在流中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这样的cpp代码 cin.clear()有疑问:

  #include< iostream>使用命名空间std;int main(无效){字符c,d;cout<<输入一个字符:"<<恩德尔cin>>C;//在这里,我将输入Ctrl + D(在Linux下为EOF)cin.clear();cout<<输入另一个字符:"<<恩德尔cin>>d;返回0;} 

我在2个系统上编译并运行了此代码:一个系统是Debian 7,带有旧版本的软件,例如g ++和库

  g ++-版本g ++(Debian 6.3.0-18 + deb9u1)6.3.0 20170516版权所有(C)2016 Free Software Foundation,Inc.这是免费软件;请参阅复制条件的来源.没有保修单;甚至不是出于适销性或针对特定目的的适用性. 

另一个系统是带有更新版本软件的Arch linux:

  g ++ --versiong ++(GCC)9.2.0版权所有(C)2019自由软件基金会,Inc.这是免费软件;请参阅复制条件的来源.没有保修单;甚至不是出于适销性或针对特定目的的适用性. 

当我运行该程序时,当我要求输入字符:"时,我输入Ctrl + D(EOF).问题是,当我在Debian 7下运行该程序时,我可以在程序要求我输入输入另一个字符:"时输入一个字符,但是在较新的系统下,程序刚刚完成,我将无法执行同样的操作./p>

在较旧的系统下,似乎 cin.clear()将清除eof位并刷新流中的EOF,而在较新的系统中, cin.clear()将清除eof一点,但保持流中的EOF不变.

这是由某些新的cpp标准引起的吗?为什么 cin.clear()在2个系统下表现不同?

解决方案

我在Ubuntu 20中也遇到了同样的问题.

我从如何在Ctrl + D后重新启动stdin找到解决方案?

终端并没有真正关闭stdin流.流仅返回无".表示EOF或其他问题.

要清除EOF,请在程序中调用clearerr(stdin)即可解决问题.

  #include< iostream>使用命名空间std;int main(无效){字符c,d;cout<<"输入字符:"<<恩德尔cin>>C;clearerr(stdin);cout<<&n; \ n输入另一个字符:"<<恩德尔cin>>d;cout<<&n; \ n \ nc ='"<<c<<"'和d ='"<<d<<‘'\ n \ n';返回0;} 

我的g ++版本是9.3.0(Ubuntu 9.3.0-17ubuntu1〜20.04)
我的操作系统是ubuntu 20 x64
我的终端是gnome终端3.36.1.1

I have a question about cin.clear(), my cpp code like this:

#include <iostream>

using namespace std;


int main(void)
{
        char c, d;
        cout << "Enter a char: " << endl;
        cin >> c;                            // here I will enter Ctrl + D (that is EOF under linux)
        cin.clear();
        cout << "Enter another char: " << endl;
        cin >> d;

        return 0;
}

I compiled and run this code under 2 systems: one system is Debian 7 with older version of software like g++ and library

 g++ --version
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

another system is Arch linux with newer version of software:

g++ --version
g++ (GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

When I run this program , I enter Ctrl + D (EOF) when it ask me to "Enter a char: ". The problem is that When I run it under Debian 7, I can Enter a char when the program asks me to "Enter another char: ", but I will not be able to do the same under newer system, the program just finished.

It seems that cin.clear() will clear eof bit and flush EOF in the stream under older system, with newer system cin.clear() will clear eof bit, but leave EOF in the stream untouched.

Is this caused by some new cpp standards? And why cin.clear() behave differenly under 2 systems?

解决方案

I have the same issue in Ubuntu 20.

I find the solution from How to restart stdin after Ctrl+D?

Terminal doesn't really close stdin stream. The stream just return "nothing" to indicate EOF or other troubles.

To clear EOF, calling clearerr(stdin) in your program may solve the problem.

#include <iostream>

using namespace std;

int main(void)
{
    char c, d;
    cout << "Enter a char: " << endl;
    cin >> c;
    clearerr(stdin);
    cout << "\nEnter another char: " << endl;
    cin >> d;

    cout << "\n\nc = '" << c << "' and d = '" << d << "'\n\n";

    return 0;
}

My g++ version is 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
My OS is ubuntu 20 x64
My terminal is gnome-terminal 3.36.1.1

这篇关于cin.clear()将EOF留在流中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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