简体   繁体   English

while循环后如何继续阅读?

[英]How to keep reading after while loop?

I have a .txt file that has a sequence of numbers and spaces on the first line that I wish to read into a vector. 我有一个.txt文件,该文件在第一行上有一系列数字和空格,我希望将其读入向量。 Then there is a '$' symbol on the next line. 然后在下一行有一个“ $”符号。 On the line after that is another line containing a sequence of numbers and spaces (like the first) that I'd like to read into another vector. 在此之后的另一行是另一行,其中包含我想读入另一个向量的数字和空格序列(如第一个)。 For example 例如

1 2 3 4 5
$
4 3 2 1 6

I've tried everything but can't keep reading after the initial while loop reads in integers. 我已经尝试了所有方法,但是在最初的while循环读取整数后无法继续读取。 How do I move past the second line and read the third? 我如何越过第二行并阅读第三行? Right now It just outputs the first line. 现在,它仅输出第一行。 Currently, this is my code: 目前,这是我的代码:

int main(int argc, const char * argv[]) {
    ifstream file(argv[1]); 
    if (file.is_open() && file.good()){
        int addMe;
        vector<int> addMeList;
        while(file>>addMe){
            cout <<addMe<<endl;
            addMeList.push_back(addMe);
        }

        string skip;
        while(file >> skip)
            cout << skip << endl;

        int searchQuery;
        vector<int> searchQueries;

        while(file>>searchQuery){
            searchQueries.push_back(searchQuery);
        }

        for (int i=0; i<searchQueries.size();i++)
        {
            cout << searchQueries[i]<<endl;
        }
    }
    return 0;

} }

Two problems: 两个问题:

  1. The first loop will cause the streams failbit to be set (when it attempts to read the '$' from the second line). 第一个循环将导致设置流故障failbit (当它尝试从第二行读取'$' )。 If that bit is set, you can't read more from the stream. 如果该位置1,则无法从流中读取更多内容。 You need to clear the stream state. 您需要清除流状态。

  2. Once you've done the above, the second loop will read the rest of the file. 完成上述操作后,第二个循环将读取文件的其余部分。

One possible solution is to read lines instead. 一种可能的解决方案是改为读取 Use eg std::getline to read a line. 使用例如std::getline读取一行。 Put the line into a std::istringstream , and read the values from that. 将行放入std::istringstream ,并std::istringstream读取值。

The program logic seems to be flawed. 程序逻辑似乎有缺陷。 Using the first while loop you read the entire file word-by-word till the very end (not till the end of line), after that trying to read again fails, which is evaluated as false , thus it never even gets into the other loops. 使用第一个 while循环,您逐字读取整个文件,直到最后(不是直到行尾),之后再次尝试读取失败,这被评估为false ,因此它甚至都不会进入另一个文件循环。 Instead, consider reading line by line using getline and then breaking it into int s using istringstream . 相反,可以考虑使用getline逐行读取,然后使用istringstream将其分解为int
Here's how I'd improve it: 这是我要改进的方法:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>                  // include this header to use istringstream
using namespace std;

int main() {
    ifstream file("text.txt");      // my test file; Replace with yours 
    if (file.is_open() && file.good()) {

        string lineIn;              // general line to read into
        int i;                      // general int to read into
        vector<int> addMeList;
        // int addMe;               // not needed anymore
        getline(file, lineIn);      // read a line 1
        istringstream istr(lineIn); // string stream we can use to read integers from
        while (istr >> i) {
            cout << i << endl;
            addMeList.push_back(i);
        }


        // string skip;              // not needed anymore 

        getline(file, lineIn);       // skips line 2

        // int searchQuery;          // not needed anymore
        vector<int> searchQueries;

        getline(file, lineIn);       // read a line 2
        istringstream istr2(lineIn); // string stream we can use to read integers from
        while (istr2 >> i) {
            searchQueries.push_back(i);
        }

        for (int i = 0; i < searchQueries.size(); i++)
        {
            cout << searchQueries[i] << endl;
        }
    }
    return 0;
}

Input file: 输入文件:

1 2 3 4 5
$
4 3 2 1 6

Output: 输出:

1
2
3
4
5
4
3
2
1
6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM