rerun the while loop for different set of data

Feb 5, 2017 at 10:54am
Hello everyone,
When I run the following code, I get a vector of 4 elements and program terminates. (The program runs successfully for the first set of data). But I want to keep running the program for the next set of data after line "#end" till another "#end" and so on for 1000s of data set. How do I do this? Any help would be appreciated greatly.
Thanks

file.dat looks like below.
1
2
3
4
#end
8
8
9
4
3
#end
9
8
8
2
and so on.
1
2
3
4
5
6
7
8
9
  int main(){
    fstream myfile("file.dat");
    double x;
    while (myfile >> x) {
      inputvec.push_back(x);
    }
    // do things with input vectors
  }
  
Feb 5, 2017 at 11:52am
After the end of the while loop above, the stream myfile will have a 'fail' status.

You could reset that, and read and discard the line "#end":
1
2
    myfile.clear();          // reset status flags
    myfile.ignore(20, '\n'); // read and discard until end of line 


Then the process can be repeated for the next set of data. Enclose the required code inside an outer loop. When should the outer loop stop? Possibly when the number of elements added to the vector is zero.


Feb 5, 2017 at 9:49pm
Thanks Chervil. It worked.
Topic archived. No new replies allowed.