Hi!,
I have a text file that contains a 2 dimensional matrix, and I will have to deal with many different files with different matrix dimensions, so I decided to use vector of vectors as they double their size automatically. the problem is that I don't know how to get every line from the file in a separate row of the vector and loop on the cols, and then increment to the next row

``````int rows = 2; // just an initial size and I rely on doubling the size automatically
int cols = 2;

vector<vector<float> > initMatrix(rows,vector<float> (cols));

ifstream myfile("a.text");
if(!myfile.is_open())
{
//if file is not opened
cout<<"Error opening the file"<<endl;
system("pause");

}

while (myfile)
{
// I don't know how to get every line from file and loop on the cols. while the cols. size and rows size will change frequently
}
myfile.close();``````

thanks.

## All 4 Replies

try this:

``````int row = 0;
float data = 0.00;
vector<vector<float> > initMatrix(5, vector<float>(cols));

while(myfile)
{
// --Handle 1D element allocation--
//Resize every 5 loop iterations (why not? it's more efficient than re-sizing every time)
//This is make sure you have a 1D element allocated in which to 'push_back()' to

// **Make sure the initMatrix vector is initialized to at least 5 1D elements**
if(!(row%5))
{
initMatrix.resize(row+5);
}

//since you have a set number of columns, we can make a known number of reads per line
//this will assume three pieces of data per line (3 columns)
for(int i=0; i<3; i++)
{
infile >> data;
initMatrix[row].push_back(data);
}

row++;
}``````

try this:

``````int row = 0;
float data = 0.00;

while(myfile)
{
//since you have a set number of columns, we can make a known number of reads per line
//this will assume three pieces of data per line (3 columns)
for(int i=0; i<3; i++)
{
infile >> data;
initMatrix[row].push_back(data);
}

row++;
}``````

thanks for ur reply, but unfortunately both no. of columns and rows are unknown.

You'll want to read a full line, then break it down into columns with something like stringstream:

``````#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
std::ifstream in("test.txt");
std::vector<std::vector<int> > v;

if (in) {
std::string line;

while (std::getline(in, line)) {
v.push_back(std::vector<int>());

// Break down the row into column values
std::stringstream split(line);
int value;

while (split >> value)
v.back().push_back(value);
}
}

for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++)
std::cout << v[i][j] << ' ';

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

You'll want to read a full line, then break it down into columns with something like stringstream:

``````#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
std::ifstream in("test.txt");
std::vector<std::vector<int> > v;

if (in) {
std::string line;

while (std::getline(in, line)) {
v.push_back(std::vector<int>());

// Break down the row into column values
std::stringstream split(line);
int value;

while (split >> value)
v.back().push_back(value);
}
}

for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++)
std::cout << v[i][j] << ' ';

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

Thank u very much, it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.