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

      1. <small id='h6egC'></small><noframes id='h6egC'>

        .eof() 循环不起作用

        时间:2023-08-27
            • <bdo id='VZab9'></bdo><ul id='VZab9'></ul>
                <tbody id='VZab9'></tbody>

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

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

              • <tfoot id='VZab9'></tfoot>
                <i id='VZab9'><tr id='VZab9'><dt id='VZab9'><q id='VZab9'><span id='VZab9'><b id='VZab9'><form id='VZab9'><ins id='VZab9'></ins><ul id='VZab9'></ul><sub id='VZab9'></sub></form><legend id='VZab9'></legend><bdo id='VZab9'><pre id='VZab9'><center id='VZab9'></center></pre></bdo></b><th id='VZab9'></th></span></q></dt></tr></i><div id='VZab9'><tfoot id='VZab9'></tfoot><dl id='VZab9'><fieldset id='VZab9'></fieldset></dl></div>
                1. 本文介绍了.eof() 循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从文件中读取数字并将它们放入数组中.现在,当我运行该程序时,它会打印 8 个数字,然后该行结束并打印相同的 8 个数字.这是一个永无止境的循环.我做错了什么?

                  I'm trying to read in numbers from a file and putting them into an array. Right now when I run the program it prints 8 numbers, then the line ends and prints the same 8 numbers. It's in a never ending loop. What am I doing wrong?

                  #include <iostream>                                                             
                  #include <fstream>                                                              
                  using namespace std;                                                            
                  
                  int main()                                                                      
                  {                                                                               
                    int num;                                                                      
                    ifstream infile;                                                              
                    infile.open("euler8Nums.txt");                                                
                      infile >> num;//must attempt to read info prior to an eof() test            
                      while(!infile.eof()){                                                       
                        cout << num << endl;                                                      
                        infile >> num;                                                            
                      }                                                                           
                      infile.close();                                                             
                      return 0;                                                                   
                  }  
                  

                  推荐答案

                  一般来说,不要使用 .eof().bad().只需检查流本身的状态

                  In general, don't use .eof() or .bad(). Just check the state of the stream itself

                  while (infile >> num)
                      cout << num << endl;
                  

                  如果流解析输入失败,则不会设置 eof 标志,然后流将停止运行,直到状态被清除.如果您改为检查 bad,它将一直持续到解析失败,但会在 EOF 处出错.所以只需检查流是否仍然是 .good(),(当它处于 while 循环时这是隐式的).

                  The eof flag will not be set if the stream fails to parse an input, and then the stream will cease to operate until the state is cleared. If you checked bad instead, it would go until it failed to parse, but would bug out at the EOF. So just check if the stream is still .good(), (which is implicit when it's in a while loop).

                  在您的情况下,这是一个无限循环,因为文件没有打开,然后您正在尝试读取数字,但是读取什么都不做,因为文件未打开不开.因此它永远不会读取 eof,从而无限循环.

                  In your case, it's an infinite loop because the file did not open and then you are attempting to read numbers, but the reading is doing nothing because the file isn't open. Thus it never reads the eof, thus infinite loop.

                  这篇关于.eof() 循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何以简单的方式从 C++ 的循环中删除最后一个逗号? 下一篇:cin 在一个 while 循环中

                  相关文章

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

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

                      • <bdo id='xhNYu'></bdo><ul id='xhNYu'></ul>
                      <tfoot id='xhNYu'></tfoot>

                    1. <legend id='xhNYu'><style id='xhNYu'><dir id='xhNYu'><q id='xhNYu'></q></dir></style></legend>