I am trying to read input from a text file into a 2-D vector where each of the rows should represent a vector itself. The first entry from each row should tell how many columns are in the row, then populate the column vectors, and the row vector.
However, I am getting some errors in my output.
In code form, I have tried to do this as
Code:
#include<iostream>
#include<fstream>
#include<vector>
#define Numrow 10 // clinical data sets
typedef std::vector<double> vec;
typedef std::vector<vec> vec2;
int main()
{
std::ifstream infile1("data1.txt");
vec2 Timerow(Numrow);
int n; // number of timepoints
if (infile1.is_open())
{
for (int i = 0; i < Numrow; ++i)
{
vec tdata;
infile1 >> n;
double t0;
for (int j = 0; j < n; ++j)
{
infile1 >> t0;
tdata.push_back(t0);
}
Timerow.push_back(tdata);
}
for (vec tdata : Timerow) std::cout << tdata.size() << std::endl;
}
else
{
std::cerr << "The file is not open" << std::endl;
}
}
Data.txt
2 0.1 0.2
3 0.2 0.3 0.4
4 0.3 0.4 0.5 0.6
5 0.4 0.5 0.6 0.7 0.8
6 0.5 0.6 0.7 0.8 0.9 0.10
2 0.1 0.2
3 0.2 0.3 0.4
4 0.3 0.4 0.5 0.6
5 0.4 0.5 0.6 0.7 0.8
6 0.5 0.6 0.7 0.8 0.9 0.10
When I try to test the number of columns on each row I get this output:
0
0
0
0
0
0
0
0
0
0
2
3
4
.
.
.
6
why am I getting the first 10 zeros? Is there something I am doing wrong? If so, where and how can I fix it?
Thanks