运行文件直到* string *然后读取列直到* no decimal *

时间:2015-06-16 15:34:05

标签: c++

我的文件看起来像

some header
this is the first block
                         1    2    3    4
                         5    6    7    8
                         9   10   11   12
this is the second block
                         1    4    7   10
                         2    5    8   11
                         3    6    9   12
this is the third block
                         1    2    3    4
                         5    6    7    8
                         9   10   11   12

我想阅读这个文件并检查单词" first"," second"和#34;第三"为了将以下数字块读入数组,我可以稍后绘制它们。例如,我只想读取第二个块的列1和列2。主要问题是我无法完成读入,直到第三个块开始。它在秒块的第一行之后停止读取。简单来说,我的代码看起来像这样。:

#include <string>
#include <fstream>
#include <istream>
#include <vector>

std::string line;
std::vector<double> vector1;
std::vector<double> vector2;
double v1;
double v2;
double v3;
double v4;

ifstream infile ("myfile.txt");
while (std::getline(infile, line)){ 
  if (line.find("second",0) != std::string::npos){    // when "second" is found start to read block.
    while (line.find_first_of("123456789.") != std::string::npos){    // while the next line is not a number continue reading. THIS DOESN'T WORK !
      infile >> v1 >> v2 >> v3 >> v4;
      vector1.push_back(v1);
      vector2.push_back(v2);
      std::getline(infile, line);
    }
  }
}
infile.close();

cout << "Vector1" << "  " << "Vector2" << endl;
for (unsigned int i = 0; i < vector1.size(); i++){
  cout << vector1[i] << "  " << vector2[i] << endl;
}

预期结果将是:

Vector1  Vector2
1        4
2        5
3        6

但我明白了:

Vector1  Vector2
1        4

3 个答案:

答案 0 :(得分:1)

您的代码存在两个问题。 第一个是读完数字之后,&#34;读取光标&#34;在行结束前停留。然后,当你调用getline时,你最终得到一个空行。将线读取方法与其他输入方法混合时,这是一个常见问题。混合使用和扫描,以及混合流时也会发生这种情况。 &GT;&GT;运算符和getline。

第二个是您正在读取整行,然后再次读取文件中的数字。如果您读取了一行文本,则必须从您读取的那一行读取数字,而不是从文件中获取新的输入。所以试试这个:

std::getline(infile, line);
while (line.find_first_of("123456789.") != std::string::npos) {
  std::stringstream stream(line);
  stream >> v1 >> v2 >> v3 >> v4;
  vector1.push_back(v1);
  vector2.push_back(v2);
  std::getline(infile, line);
}

答案 1 :(得分:1)

我很惊讶你得到了那个结果。如果行包含&#39; second&#39;,则不包含任何数字,您无法输入if。

我会像这样改变身体:

ifstream infile("myfile.txt");
while (std::getline(infile, line)){
    if (line.find("second", 0) != std::string::npos)
    {    // when "second" is found start to read block.
        while (infile){    // while the next line is not a number continue reading. THIS DOESN'T WORK !

            infile >> v1 >> v2 >> v3 >> v4;
            if (!infile)
                break;
            vector1.push_back(v1);
            vector2.push_back(v2);
            std::getline(infile, line);
        }
    }
}

所以,当你发现第二个&#39;继续阅读数字。当数字读数失败时if (!infile)退出循环

答案 2 :(得分:1)

这应该修复双读内部和退出循环,

ifstream infile ("myfile.txt");
while (std::getline(infile, line)) { 
    if (line.find("second",0) != std::string::npos) {
        while (infile >> v1 >> v2 >> v3 >> v4) {
            vector1.push_back(v1);
            vector2.push_back(v2);
            // note getline is removed, else double reads on break.
        }
        // this may be needed if you plan on reading anything else.
        infile.clear();
    }
}