Read matrix from file


I'm trying to make matrix to be read from file but the followin mistake occurs " no matching function for call to 'std::vector<std::vector<int> >:: push_back(size_t&)". What can I do and can the matrix be read this way at all?
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
 const char* FILE_NAME = "myfile.txt";
int fileIn(const char* FILE_NAME, size_t var)
{
           std::vector<std::vector<int>> arr;
            
             std::ifstream file(FILE_NAME);
 
                if (!file.is_open()) {
                    throw ERROR_FILE;
                }
                
               while(!file.eof())
               {
                    file >> var;
                    arr.push_back(var); //Right Here
                    std::cout << std::endl;
            
               }
                size_t N = arr.size();
               
               file.close();
                 arr.clear();
                 return 0;
}


File:
1 23 1 4
4 8 2 4
1 3 4 2
2 2 6 1
The problem with an empty 2D vector you can't simply push back a value. As you can with a 1D vector.

1. You should create a temporary vector to hold a row's data and push back that temp vector back into your vector.

OR

2. Size the 2D vector when you create it and use operator[]/.at to fill the vector with the read values.
Using the file data you provided; a "quick and dirty" sloppy bit of 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

enum ERROR_CODE { SUCCESS, FAIL };

ERROR_CODE readFile(std::vector < std::vector<int>>& arr, std::string fileName);

int main()
{
   std::string file_name { "myfile.txt" };

   std::vector<std::vector<int>> arr;

   ERROR_CODE ret { readFile(arr, file_name) };

   if (ret != FAIL)
   {
      for (auto const& row_itr : arr)
      {
         for (auto const& col_itr : row_itr)
         {
            std::cout << col_itr << ' ';
         }
         std::cout << '\n';
      }
   }
}

ERROR_CODE readFile(std::vector<std::vector<int>>& arr, std::string fileName)
{

   std::ifstream file(fileName);

   if (!file.is_open())
   {
      return FAIL;
   }

   std::string temp_line;

   while (std::getline(file, temp_line))
   {
      std::vector<int> temp_vec;

      std::istringstream iss { temp_line };

      int temp_val;

      while (iss >> temp_val)
      {
         temp_vec.push_back(temp_val);
      }

      arr.push_back(temp_vec);
   }
   return SUCCESS;
}
1 23 1 4
4 8 2 4
1 3 4 2
2 2 6 1
Thanks! But is it possible to do it without using constructors and classes? I'm beginner and want to use simpliest ways
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
	std::ifstream ifs("myarray.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<std::vector<int>> arry2d;

	for (std::string line; std::getline(ifs, line); ) {
		std::istringstream iss(line);
		std::vector<int> vi;

		for (int i; iss >> i; vi.push_back(i));
		arry2d.push_back(vi);
	}

	for (const auto& v : arry2d) {
		for (const auto n : v)
			std::cout << n << "  ";

		std::cout << '\n';
	}
}


Which displays:


1  23  1  4
4  8  2  4
1  3  4  2
2  2  6  1

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;


vector< vector<int> > fileIn( const char* filename )
{
   vector< vector<int> > arr;
   ifstream in( filename );
   if ( in )
   {
      for ( string line; getline( in, line ); )
      {
         stringstream ss( line );
         vector<int> row;
         for ( int i; ss >> i; ) row.push_back( i );
         arr.push_back( row );
      }
   }
   else
   {
      cerr << "Boo! No such file\n";
   }
   return arr;
}


int main()
{
   const char* FILE_NAME = "myfile.txt";
   vector< vector<int> > A = fileIn( FILE_NAME );
 
   for ( auto & row : A )
   {
      for ( auto e : row ) cout << e << '\t';
      cout << '\n';
   }
}

Last edited on
Thanks guys
Topic archived. No new replies allowed.