Reading file and put info in 2d vector

Hey I've been practicing how to go on and read from a specified file containing a matrix of numbers and trying to put that file info into a 2d vector and need some help and printing it out. I've just recently learned about 2d arrays, so it is still confusing somewhat.

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
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void read_data(ifstream& , vector <vector <int> >& );
void print_data(const vector <vector<int> >&);

int main()
{
	ifstream infile;
	vector<vector<int>> A(5, vector<int>(10));
	infile.open("matrix.dat");
	read_data(infile, A);
        print_data(A);
}

void read_data(ifstream& is, vector <vector <int> >& m)
{
    int item;
    while (!is.eof())
    {
        is >> item;
        for (int i = 0; i < 5; i++)
        {
            vector<int>temp;
            for (int j = 0; j < 10; i++)
            {
                temp.push_back(item);
            }
            m.push_back(temp);
        }
    }
}

void print_data(const vector <vector<int> >& m)
{
    for (int i = 0; m.size(); i++)
    {
        for (int j = 0; j < m[i].size(); j++)
        {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}


P.S the matrix has the following info contained:
64 -79 -5 81 22 86 1 46 -79 19
33 -90 -83 61 -68 31 44 -53 -41 65
17 -49 -15 -97 65 -28 -20 20 -63 -72
75 77 -51 -53 35 47 -67 -63 -30 -47
32 -97 -61 48 40 -52 -43 -16 6 8
Last edited on
So do you have a question or problem?

Do you know that because you set the sizes of the vector in main() the push_backs in the function add more elements to the vector, increasing the vector sizes, leaving half of the vectors uninitialized?

I simply have a question onto how I can store the info from the file into a 2d vector. I'm somewhat closer now as I changed the read_data function to:

1
2
3
4
5
6
7
8
9
void read_data(ifstream& is, vector <vector <int> >& m)
{
    int item;
    while (!is.eof())
    {
        is >> item;                  
        m.push_back(vector<int>{item});                       
    }
}

doesn't entirely work yet as the vector subscript is now out of range, but presents the numbers from the file. Albeit the output of those numbers are messed up as they're displayed in a vertical fashion instead of the way it is supposed to be like in the file.
Last edited on
No, you're not really much closer.

Why are you setting the size of the vectors in main()?

Do you realize that push_back adds "new" elements?

You need to decide which is better, letting the "file contents" determine the size of the vectors, or preset the vector sizes and "hope" you used the proper preset sizes.

If you go with preset sizes, remember that a vector always knows it's size, and you can use ranged based loops to read the file.

Something like the following:
1
2
3
4
5
    for(auto& outer : data)
    {
        for(auto& inner : outer)
            inFile >> inner;
    }




Ah! thank you! by any chance, do you have a link to where I can learn this method you used? I've never seen something like this before. I don't want to simply take this answer and have no idea what it does. I wish to learn what it does. I know the auto part, but not the rest.
Last edited on
Look up "C++ ranged based loops". It's basically doing what the loops in your current print function are doing, but with less chances of mistakes.

Also, IMO, using pre-sized vectors are not the best way to read files. I prefer methods that let the file contents determine the sizes of the vectors.

Okay, thank you ever much!
Matrices are generally more than 2D arrays because they generally have a defined size known at the time they are created/constructed.

The following shows how this can be taken advantage of and, while not shown, leads to the value of encapsulating the matrix elements together with the matrix size in a struct or class.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <vector>

#include <iomanip>

using namespace std;

void read_data(ifstream& , vector <vector <int> >&, int, int );
void print_data(const vector <vector<int> >&, int, int);

int main()
{
    
    
    int no_rows = 5;
    int no_cols = 10;
    vector<vector<int>> A(no_rows, vector<int>(no_cols));
    
    
    ifstream infile;
    infile.open("matrix_1.dat");
    
    
    read_data(infile, A, no_rows, no_cols);
    print_data(A, no_rows, no_cols);
}

void read_data(ifstream& is, vector <vector <int> >& m, int ROWS, int COLS)
{
    int temp = 0;
    
    for (int row = 0; row < ROWS; row++)
    {
        for (int col = 0; col < COLS; col++)
        {
            is >> temp;
            m[row][col] = temp;
        }
    }
}

void print_data(const vector <vector<int> >& m, int ROWS, int COLS)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            cout << std::setw(6) << m[i][j];
        }
        cout << endl;
    }
}


  
    64   -79    -5    81    22    86     1    46   -79    19
    33   -90   -83    61   -68    31    44   -53   -41    65
    17   -49   -15   -97    65   -28   -20    20   -63   -72
    75    77   -51   -53    35    47   -67   -63   -30   -47
    32   -97   -61    48    40   -52   -43   -16     6     8
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.