<small id='Yfw5V'></small><noframes id='Yfw5V'>

  • <legend id='Yfw5V'><style id='Yfw5V'><dir id='Yfw5V'><q id='Yfw5V'></q></dir></style></legend>
      <i id='Yfw5V'><tr id='Yfw5V'><dt id='Yfw5V'><q id='Yfw5V'><span id='Yfw5V'><b id='Yfw5V'><form id='Yfw5V'><ins id='Yfw5V'></ins><ul id='Yfw5V'></ul><sub id='Yfw5V'></sub></form><legend id='Yfw5V'></legend><bdo id='Yfw5V'><pre id='Yfw5V'><center id='Yfw5V'></center></pre></bdo></b><th id='Yfw5V'></th></span></q></dt></tr></i><div id='Yfw5V'><tfoot id='Yfw5V'></tfoot><dl id='Yfw5V'><fieldset id='Yfw5V'></fieldset></dl></div>

        • <bdo id='Yfw5V'></bdo><ul id='Yfw5V'></ul>
      1. <tfoot id='Yfw5V'></tfoot>
      2. 在 C++ 中被 EOF 停止后如何恢复输入流?

        How to resume input stream after stopped by EOF in C++?(在 C++ 中被 EOF 停止后如何恢复输入流?)

            • <bdo id='KoQ8J'></bdo><ul id='KoQ8J'></ul>

                <legend id='KoQ8J'><style id='KoQ8J'><dir id='KoQ8J'><q id='KoQ8J'></q></dir></style></legend>

                <small id='KoQ8J'></small><noframes id='KoQ8J'>

                <i id='KoQ8J'><tr id='KoQ8J'><dt id='KoQ8J'><q id='KoQ8J'><span id='KoQ8J'><b id='KoQ8J'><form id='KoQ8J'><ins id='KoQ8J'></ins><ul id='KoQ8J'></ul><sub id='KoQ8J'></sub></form><legend id='KoQ8J'></legend><bdo id='KoQ8J'><pre id='KoQ8J'><center id='KoQ8J'></center></pre></bdo></b><th id='KoQ8J'></th></span></q></dt></tr></i><div id='KoQ8J'><tfoot id='KoQ8J'></tfoot><dl id='KoQ8J'><fieldset id='KoQ8J'></fieldset></dl></div>
                  <tbody id='KoQ8J'></tbody>
                  <tfoot id='KoQ8J'></tfoot>
                1. 本文介绍了在 C++ 中被 EOF 停止后如何恢复输入流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想编写一个程序,以便从终端获取两组整数输入并计算两个总和.我的意图是通过EOF(按Ctrl + D)将两组输入分开.这是我的代码:

                  I want to write a program so that it takes two sets of integer input from terminal and computes two sums. My intention is to separate the two sets of input by EOF (pressing Ctrl+D). Here is my code:

                  #include <iostream>
                  using namespace std;
                  
                  int main(){
                      int i,sum=0;
                      while((cin>>i).good())
                          sum+=i;
                      cout<<"Sum 1 is "<<sum<<endl;
                      cin.clear();
                      sum=0;
                      while((cin>>i).good())
                          sum+=i;
                      cout<<"Sum 2 is "<<sum<<endl;
                      return EXIT_SUCCESS;
                  }
                  

                  编译后的程序对于第一组整数输入运行良好.但是当我按下 Ctrl+D 时,第一个总和被计算并打印出来,并且没有采取任何进一步的输入,将第二个总和打印为 0.所以基本上第二个 while 循环在一开始就失败了,即使 cin.iostate 有在它之前被设置好.那么为什么会发生这种情况呢?我应该如何更改程序以便第二个 while 循环按预期进行?

                  The compiled program worked fine for the first set of integer inputs. But as soon as I pressed Ctrl+D, the first sum was computed and printed and, without taking any further input, printed the second sum as 0. So basically the second while loop failed at the very beginning, even though cin.iostate had been set to good before it. So why did this happen? How should I change the program so that the second while loop would proceed as intended?

                  推荐答案

                  当您在 tty 处于规范模式时使用 Ctrl-D 时,它会关闭系统级管道.无论您对 std::cin 做什么,都不会将流恢复到良好状态.如果您坚持使用 Ctrl-D 来表示序列结束(这是一个不寻常的界面,可能最好避免),您需要使用 tcgetattr() 清除 ICANON 标志tcsetattr() 用于标准输入流(文件描述符 0).您将需要处理任何控制字符.

                  When you use Ctrl-D while the tty is in canonical mode it closed the system level pipe. Whatever you do to std::cin won't restore the stream into a good state. If you insist in using Ctrl-D to signal the end of the sequence (which is an unusual interface and probably best avoided), you'll need to clear the ICANON flag using tcgetattr() and tcsetattr() for the standard input stream (file descriptor 0). You will need to deal with any control characters.

                  阅读第一次失败可能更容易,clear() 状态和 ignore() 有问题的字符或检查它们是否有一个特定的值.

                  It is probably easier to read up to the first failure, clear() the state and either ignore() the offending character(s) or check that they have a specific value.

                  这篇关于在 C++ 中被 EOF 停止后如何恢复输入流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                      <tbody id='W6c8S'></tbody>
                    1. <i id='W6c8S'><tr id='W6c8S'><dt id='W6c8S'><q id='W6c8S'><span id='W6c8S'><b id='W6c8S'><form id='W6c8S'><ins id='W6c8S'></ins><ul id='W6c8S'></ul><sub id='W6c8S'></sub></form><legend id='W6c8S'></legend><bdo id='W6c8S'><pre id='W6c8S'><center id='W6c8S'></center></pre></bdo></b><th id='W6c8S'></th></span></q></dt></tr></i><div id='W6c8S'><tfoot id='W6c8S'></tfoot><dl id='W6c8S'><fieldset id='W6c8S'></fieldset></dl></div>
                    2. <legend id='W6c8S'><style id='W6c8S'><dir id='W6c8S'><q id='W6c8S'></q></dir></style></legend>
                        <bdo id='W6c8S'></bdo><ul id='W6c8S'></ul>
                        <tfoot id='W6c8S'></tfoot>

                          <small id='W6c8S'></small><noframes id='W6c8S'>