Reading text file into array

Hi everyone. I new to c++ programming. I would like to ask regarding reading an array from text file. Given an input data as follows:
Input data (in .txt):
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 line represents each solution. For example line 1 illustrates the initial route 1, line 2 is the initial route 2 and so on. I had done with coded in which store the particular data into array (read line by line).
The question is, what if later on i want to solve the following question.
initialroute[0][3]= 5
initialroute [9][3] = 9
Hopefully you understand what i meant. Hoping anyone could help me.
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	    int route = 0;
            string line;
            stringstream lineStream;
			
	    ifstream inpInitialRoute("Route.txt");


            //read lines
            while( getline(inpInitialRoute, line) )
            {
                lineStream.clear();
                lineStream.str(line);
                cout << "Initial route" << route++
					<< "=	" << lineStream.str() << std::endl;
            }

 	    inpInitialRoute.close();

	    system("pause");

	    return 0;
}
What you need is a 2D container, let’s say a std::vector<std::vector>> or an int * myarr[].

Anyway, if you can guarantee you have the same amount of data in each line, the code becomes much easier.
If you plan to use numbers as... numbers, you can add leading zeroes to check if all the lines are equal.

In that case, Route.txt becomes:
00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 04 08 09 05 11 10 02 12 07 03 06 01 00
00 05 08 09 04 11 10 02 12 07 03 06 01 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 09 08 05 04 11 10 02 12 07 03 06 01 00
00 10 11 09 08 05 04 01 07 03 06 12 01 00
00 11 10 09 08 05 04 01 07 03 06 12 01 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00



And then, adding a column where it’s missing:
00 08 09 05 04 11 10 02 12 07 03 06 01 00 00
00 04 08 09 05 11 10 02 12 07 03 06 01 00 00
00 05 08 09 04 11 10 02 12 07 03 06 01 00 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00
00 08 09 05 04 11 10 02 12 07 03 06 01 00 00
00 09 08 05 04 11 10 02 12 07 03 06 01 00 00
00 10 11 09 08 05 04 01 07 03 06 12 01 00 00
00 11 10 09 08 05 04 01 07 03 06 12 01 00 00
00 00 08 09 05 04 11 10 02 12 07 03 06 01 00


To get further help, I think it’s better if you clarify if this is an assignment or not.
Hi Enoizat.. sorry i forgot to told above that this is an assignment. You said that what i need is 2d container in order to display those answer. Can i know either the coded i do before is right or not. Do 2d container needed after we read line by line. Sorry asking. I new to c++ and do not know much. Thank you.
So, do you know about std::stringstream and you don’t know about 2D containers?
If your teacher is really focusing on standard library instead of boring memory management, you’re really a lucky guy.
Are you sure you haven’t ever seen something like:
int * myarr[];
or
int myarr[][];
or
std::vector<std::vector<int>> myvec; ?
Haven’t you ever done exercises with matrices?

Anyway, moving from your code, one possible logic to read a table inside a 2D container can be:
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::ifstream inp_initial_route("Route.txt");
    if(!inp_initial_route) {
        std::cout << "Cannot open Route.txt.\n";
        return 0;
    }

    // TODO: define a 2D container here

    for(std::string line; std::getline(inp_initial_route, line); /**/) {
        std::istringstream line_stream { line };
        // Now you have another stream ("line_stream"), which is a C++ stream
        // exactly like "inp_initial_route".
        // In "line_stream" is stored a single line from "inp_initial_route".
        // You could add a inner loop here which takes the numbers from
        // "line_stream" and stores them into a 'row' of your 2D container.
    }

    return 0;
}


But, if you don’t know about 2D containers, perhaps you’d better sooner ask help about that.

If you are still stuck, please ask again: there are plenty of people willing to help on this forum.

Topic archived. No new replies allowed.