简体   繁体   中英

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. 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. 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). If that bit is set, you can't read more from the stream. 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. Put the line into a std::istringstream , and read the values from that.

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. Instead, consider reading line by line using getline and then breaking it into int s using istringstream .
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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