Reading text file and save in vector

I have a text file consisting of 5 lines.
int
int
int
unknown number of int separated by space
unknown number of int separated by space

This is a sample of my text file:
8
0
63
1 2 3 5 6 7 9 13 14 15 17 20 22 23 24 26 27 28 30 33 38 39 41 42 43 44 47 48 50 52 53 56 57 58 59 60 61 62
0 8 16 25 34 35 36 45 54 63

NOTE: the string in 4th line is from 1 to 62
the string in 5th line is from 0 to 63

Each line represents:
height(int)
start(int)
goal(int)
obstacles(vector of int)
pathCells(vector of int)

I need to read each line and save it in the corresponding variable type.
This is my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 		//////////////////////////////
		ifstream MyReadFile;
		MyReadFile.open("pathFile.txt",ios::in);
		int cell;
		/// Line 1: environment size
		MyReadFile >> height;
		width=height;
		/// Line 2: start
		MyReadFile >> start;
		/// Line 3: goal
		MyReadFile >> goal;
		/// Line 4: obstacles cells
		string obstaclesStr;
		getline (MyReadFile,obstaclesStr);
		stringstream iss1( obstaclesStr );
		vector<int> obstacles;
		while ( iss1 >> cell )
			obstacles.push_back( cell );
		/// Line 5: path cells
		string pathStr;
		getline (MyReadFile,pathStr);
		stringstream iss2( pathStr );
		vector<int> path;
		while ( iss2 >> cell )
			path.push_back( cell );
		///
		MyReadFile.close();
		cout<<"Size="<<height<<endl;
		cout<<"Start="<<start<<endl;
		cout<<"Goal="<<goal<<endl;
		cout<<"The obstacles are:"<<endl;
		for(int i=0; i<obstacles.size();i++)
			cout<<obstacles[i]<<" ";
		cout<<endl;
		
		cout<<"The path is:"<<endl;
		for(int i=0; i<path.size();i++)
			cout<<path[i]<<" ";
		cout<<endl;

This the output:
Size=8
Start=0
Goal=63
The obstacles are:

The path is:
1 2 3 5 6 7 9 13 14 15 17 20 22 23 24 26 27 28 30 33 38 39 41 42 43 44 47 48 50 52 53 56 57 58 59 60 61 62

That is, the obstacles vector is empty, and the path vector takes the values that are supposed to be for obstacles.
How can I correct this?
There is a new line left in the line after reading in the value of goal;
we need to skip over that before using std::getline() to read the next line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

// get integers in one line of input
std::vector<int> get_integers_from( const std::string& line )
{
    std::istringstream stm(line) ;

    std::vector<int> vec ;

    int n ;
    while( stm >> n ) vec.push_back(n) ; // coukld use istream_iterator<int> instead

    return vec ;
}

int main()
{
    if( std::ifstream file{"pathFile.txt"} )
    {
       // read int height, start, goal, then extract and throw away white space
       int height, start, goal ;
       file >> height >> start >> goal >> std::ws ;

       std::string line ;

       // read one line and make obstacles vector
       std::getline( file, line ) ;
       const auto obstacles = get_integers_from(line) ;

       // read one more line and make path_cells vector
       std::getline( file, line ) ;
       const auto path_cells = get_integers_from(line) ;
    }
}
Thank you. It works.
Topic archived. No new replies allowed.