• <small id='7nfdp'></small><noframes id='7nfdp'>

    <legend id='7nfdp'><style id='7nfdp'><dir id='7nfdp'><q id='7nfdp'></q></dir></style></legend>

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

        来自文件的简单 C++ 输入......如何?

        时间:2023-08-28
        1. <tfoot id='lED1a'></tfoot>
            <tbody id='lED1a'></tbody>

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

                <i id='lED1a'><tr id='lED1a'><dt id='lED1a'><q id='lED1a'><span id='lED1a'><b id='lED1a'><form id='lED1a'><ins id='lED1a'></ins><ul id='lED1a'></ul><sub id='lED1a'></sub></form><legend id='lED1a'></legend><bdo id='lED1a'><pre id='lED1a'><center id='lED1a'></center></pre></bdo></b><th id='lED1a'></th></span></q></dt></tr></i><div id='lED1a'><tfoot id='lED1a'></tfoot><dl id='lED1a'><fieldset id='lED1a'></fieldset></dl></div>
              • <legend id='lED1a'><style id='lED1a'><dir id='lED1a'><q id='lED1a'></q></dir></style></legend>
                  <bdo id='lED1a'></bdo><ul id='lED1a'></ul>
                  本文介绍了来自文件的简单 C++ 输入......如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个文件:

                  P 0.5 0.6 0.3
                  30 300
                  80 150
                  160 400
                  200 150
                  250 300
                  T
                  r  45 0 0
                  s 0.5 1.5 0 0
                  t 200 –150
                  .
                  .
                  .
                  

                  当我读到P"时,我知道接下来会出现 3 个浮点数.接下来是有限数量的 X 和 Y 坐标.数字会有所不同,直到达到我必须识别的T".然后可能有一个 'r'、's' 或 't' 后跟一些值.

                  When I read in 'P' I know that 3 floats will follow. That will be followed by a finite number of X and Y coordinates. The number will vary until a 'T' is reached which I have to recognize. Then there could be an 'r', 's' or 't' followed by some values.

                  无论如何,我知道如何识别P",然后接收 2 个浮点数,但后来我知道我必须为 X 和 Y 坐标设置一个 while 循环,当我到达T"时该循环将停止.我对 C++ 的了解不够,无法让循环停止并识别T",然后再做其他事情.

                  Anyways I know how to recognize 'P' and then take in the 2 floats but then I know I have to have a while loop for the X and Y coordinates which will stop when I get to a 'T'. I do not know enough about C++ to make the loop stop and recognize the 'T' and then do something else.

                  一个解释的例子将不胜感激.提前致谢!

                  An example to explain would be appreciated. Thanks in advance!

                  推荐答案

                  我将向您展示我认为正确的 C++ 方法.首先定义一个类来表示您的第一行并执行其 IO:

                  I'll show you what I think it's the proper C++ way of doing this. First define a class for representing your first line and for doing its IO:

                  struct FirstLine
                  {
                      double x, y, z;
                      friend std::istream & operator>>(std::istream & is, FirstLine & data)
                      {
                          std::string line, ignore;
                          std::getline(is, line);
                          std::istringstream iss(line);
                          iss >> ignore >> data.x >> data.y >> data.z;
                          assert(ignore == "P" && iss);
                          return is;
                      }
                      friend std::ostream & operator<<(std::ostream & os, FirstLine const & data)
                      {
                          return os << "P " << data.x << " " << data.y << " " << data.z;
                      }    
                  };
                  

                  我已经使用断言添加了一些基本的错误检查,您可能希望在最终程序中使用更强大的功能.

                  I've added some basic error checking with assert, you'll probably want something more robust in your final program.

                  现在是中间线的类:

                  struct MiddleLine
                  {
                      double x, y;
                      friend std::istream & operator>>(std::istream & is, MiddleLine & data)
                      {
                          std::string line;
                          std::getline(is, line);
                          if(line == "T")
                              is.clear(std::ios::failbit);
                          else
                          {
                              int n = sscanf(line.c_str(), "%lf %lf", &data.x, &data.y);
                              assert(n == 2);
                          }
                          return is;
                      }
                      friend std::ostream & operator<<(std::ostream & os, MiddleLine const & data)
                      {
                          return os << data.x << " " << data.y;
                      }    
                  };
                  

                  当我们到达中间线所在部分的末尾时,我们应该会遇到一个T".在这种情况下,我们提高流的失败位,它会告诉客户端没有更多的中间行要读取.

                  When we reach the end of the section where the middle lines are we are supposed to encounter a "T". In that case we raise the fail bit of the stream, which will tell client that there are no more middle lines to read.

                  最后几行的类:

                  struct LastLine
                  {
                      std::string identifier; // r, s or t
                      std::vector<double> values;
                      friend std::istream & operator>>(std::istream & is, LastLine & data)
                      {
                          std::string line;
                          std::getline(is, line);
                          std::istringstream iss(line);
                          iss >> data.identifier;
                          assert(data.identifier == "r" || data.identifier == "s" 
                                 || data.identifier == "t");
                          std::copy(std::istream_iterator<double>(iss), 
                                    std::istream_iterator<double>(), std::back_inserter(data.values));
                          return is;
                      }
                      friend std::ostream & operator<<(std::ostream & os, LastLine const & data)
                      {
                          os << data.identifier << " ";
                          std::copy(data.values.begin(), data.values.end(),
                                    std::ostream_iterator<double>(os, " "));
                          return os;
                      }      
                  };
                  

                  最后几行比较复杂,因为我们不知道每一行有多少个值,所以我们尽可能多地读取.

                  Last lines are more complicated becase we don't know how many values are in each, so we just read as many as we can.

                  那是棘手的部分.现在我们的 main 函数将简单地读取第一行,然后是未知数量的中间行,最后是未知数量的最后一行:

                  That was the tricky part. Now our main function will simply read one first line, then an unknown number of middle lines, and finally an unknown number of last lines:

                  int main()
                  {
                      std::string const data = "P 0.5 0.6 0.3
                  
                                               "30 300
                  "
                                               "80 150
                  "
                                               "160 400
                  "
                                               "200 150
                  "
                                               "250 300
                  "
                                               "T
                  "
                                               "r  45 0 0
                  "
                                               "s 0.5 1.5 0 0
                  "
                                               "t 200 –150";
                  
                      std::istringstream iss(data);
                  
                      FirstLine first_line;
                      iss >> first_line;
                  
                      std::vector<MiddleLine> middle_lines;
                      std::copy(std::istream_iterator<MiddleLine>(iss), 
                                std::istream_iterator<MiddleLine>(), 
                                std::back_inserter(middle_lines));
                      iss.clear();
                  
                      std::vector<LastLine> last_lines;
                      std::copy(std::istream_iterator<LastLine>(iss), 
                                std::istream_iterator<LastLine>(), 
                                std::back_inserter(last_lines));
                      assert(iss.eof());       
                  
                      std::cout << first_line << "
                  ";
                      std::copy(middle_lines.begin(), middle_lines.end(),
                                std::ostream_iterator<MiddleLine>(std::cout, "
                  "));
                      std::copy(last_lines.begin(), last_lines.end(),
                                std::ostream_iterator<LastLine>(std::cout, "
                  "));
                      return 0;
                  }
                  

                  这是你将得到的输出::

                  This is the output you'll get::

                  P 0.5 0.6 0.3
                  30 300
                  80 150
                  160 400
                  200 150
                  250 300
                  r 45 0 0
                  s 45 0 0 0.5 1.5 0 0
                  t 45 0 0 0.5 1.5 0 0 200
                  

                  我使用了一个字符串作为我的数据源,但您可能想要从文件中读取.

                  I've used a string as the source of my data but you'll probably want to read from a file.

                  仅此而已,您可以看到我没有编写一个循环.

                  And that's all, you can see that I didn't write a single loop.

                  这是键盘中的代码.

                  这篇关于来自文件的简单 C++ 输入......如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:谁能告诉我为什么这些函数没有给我一个合理范围的结果? 下一篇:C++ while 和 do while 的区别?

                  相关文章

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

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