Reading text file into array

Hi everyone, I have a text file as below. Each row represent one set of solution. 10 row indicated 10 solution/ initial route. Each solution can be read like this.
Example solution row1:
0 8 9 5 4 11 10 2 12 7 3 6 1 0
Explanation: The route start from 0 then visit 8, then 9, 5, 4, ..., until return to 0.

Now, I need to store this data into array. I had try but error occurred. I don't know how to correct/adjust the errors. Hopefully you understand what i meant. Hoping help from anyone. Thanks.

Here is the input file:
0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 4 8 9 5 11 10 2 12 7 3 6 1 0
0 5 8 9 4 11 10 2 12 7 3 6 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 9 8 5 4 11 10 2 12 7 3 6 1 0
0 10 11 9 8 5 4 1 7 3 6 12 1 0
0 11 10 9 8 5 4 1 7 3 6 12 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0

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
  #include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int i, j;
int initialRoute = 10;	


int main()
{
	//open input file
	ifstream inpInitialRoute("Route.txt");

	//assign initial route to an array
	for (i= 0; i<10; i++)
	{
		initialRoute[i] = 15;

		for (j= 0; j<15; j++)
		{
			inpInitialRoute >> initialRoute[i][j];
			//print array
			cout >> initialRoute[i][j] >> endl;
		}
	}
	//close input file
	inpInitialRoute.close();
	return 0;
}

- Use getline to read a whole line into a string.
- Put that string into a stringstream.
- Read as many integers as you can out of that stringstream and push_back each integer into a vector<int> (a 1-d array).
- When the row is complete, push_back that vector<int> into a vector<vector<int>> (i.e. a 2-d array).
Hi,

I managed to store data from a text file. However now I need to display each row.

Given data:
0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 4 8 9 5 11 10 2 12 7 3 6 1 0
0 5 8 9 4 11 10 2 12 7 3 6 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 8 9 5 4 11 10 2 12 7 3 6 1 0
0 9 8 5 4 11 10 2 12 7 3 6 1 0
0 10 11 9 8 5 4 1 7 3 6 12 1 0
0 11 10 9 8 5 4 1 7 3 6 12 1 0
0 0 8 9 5 4 11 10 2 12 7 3 6 1 0

Each row represent one set of route. I want to display each of it. For example, if i want Initial route 1. it will display whole line 1 which is 0 8 9 5 4 11 10 2 12 7 3 6 1 0. Any way to do it. I cant figure it out.

Here is the 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
40
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	int i, j;
	string line;
	string array[10][15];

	ifstream inpInitialRoute("Route.txt");

	for(int i = 0; i < 10; ++i) 
	{
		for(int j = 0; j < 15; ++j)
		{
			if(getline(inpInitialRoute, line, ';'))
			{
				array[i][j] = line;
				cout << array[i][j] << endl;
				//display initialRoute 1: 0  8  9  5  4  11  10  2  12  7  3  6  1  0
				cout << "initialRoute 1: " << line[0] << endl;
				

			}
		}
	}

	
	

	inpInitialRoute.close();

	system("pause");

	return 0;
}
In my last post I gave a sequence of 4 things to do. You have done the first of them. Try the rest.
Topic archived. No new replies allowed.