<bdo id='sVcXT'></bdo><ul id='sVcXT'></ul>

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

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

        使用 ifstream 读取文件

        时间:2023-10-18

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

                  <bdo id='1wnVf'></bdo><ul id='1wnVf'></ul>
                  本文介绍了使用 ifstream 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从文件中读取:该文件是多行的,基本上我需要检查每个单词".词是任何非空间.

                  I am trying to read from file: The file is multiline and basically i need to go over each "word". Word being anything non space.

                  示例输入文件将是:

                  Sample input file would be:

                  示例文件:

                  测试二维
                  字 3.5
                  输入
                  {

                  测试 13.5 12.3
                  另一个{
                  测试 145.4
                  }
                  }

                  test 2d
                  word 3.5
                  input
                  {

                  test 13.5 12.3
                  another {
                  testing 145.4
                  }
                  }

                  所以我尝试了这样的事情:

                  So I tried something like this:

                  ifstream inFile(fajl.c_str(), ifstream::in);
                  
                  if(!inFile)
                  {
                      cout << "Cannot open " << fajl << endl;
                      exit(0);
                  }
                  
                  string curr_str;
                  char curr_ch;
                  int curr_int;
                  float curr_float;
                  
                  cout << "HERE
                  ";
                  inFile >> curr_str;
                  
                  cout << "Read " << curr_str << endl;
                  

                  问题是当它读取新行时它只是挂起.我在测试 13.5 之前阅读了所有内容但是一旦它到达那条线,它就什么也不做.谁能告诉我我做错了什么?关于如何做到这一点的任何更好的建议???

                  The problem is when it reads new line it just hangs. I read everything before test 13.5 but once it reaches that line it doesnt do anything. Anyone can tell me what I am doing wrong? Any better suggestion on how to do this???

                  当时我基本上需要浏览文件并输入一个单词"(非白色字符).我

                  I essentially need to go through file and go one "word" (non white char) at the time. I

                  谢谢

                  推荐答案

                  您打开了一个文件 'inFile' 但正在从 'std::cin' 中读取任何特殊原因?

                  You open a file 'inFile' but are reading from the 'std::cin' any particular reason?

                  /*
                   * Open the file.
                   */
                  std::ifstream   inFile(fajl.c_str());   // use input file stream don't.
                                                          // Then you don't need explicitly specify
                                                          // that input flag in second parameter
                  if (!inFile)   // Test for error.
                  {
                      std::cerr << "Error opening file:
                  ";
                      exit(1);
                  }
                  
                  std::string   word;
                  while(inFile >> word)  // while reading a word succeeds. Note >> operator with string
                  {                      // Will read 1 space separated word.
                      std::cout << "Word(" << word << ")
                  ";
                  }
                  

                  这篇关于使用 ifstream 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:确定流结束时出现EOF问题 下一篇:如何在纯 C/C++ (cout/printf) 中显示进度指示器?

                  相关文章

                      <bdo id='Op83l'></bdo><ul id='Op83l'></ul>

                    <tfoot id='Op83l'></tfoot>

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

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