Hi everyone,
I have some troubles with reading floats from a file into a 2d vector. Basically, I have 3 coordinates (x,y,z) of some points which I want to store in a 2d vector. This is what I have so far:
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
if (argc != 2){
cout << "need a file..." << endl;
return 1;
}
int i=1;
float x,y,z;
string line;
vector< vector<float> > basis;
vector<float> row;
ifstream fin(argv[1]);
istringstream inputString(line);
while(i<4 && getline(fin, line) ){
inputString >> x >> y >> z;
row.push_back(x);
row.push_back(y);
row.push_back(z);
basis.push_back(row);
row.clear();
i++;
}
for(int l=0; l<3; l++){
for(int m=0; m<3; m++){
cout << basis[l][m] << " ";
}
cout << endl;
}
return 0;
}
The input file has the following format:
e.g.
1.33939344 3.498494944 1.33891
8.8484844 12.39398 4.59582111
23.9333 7.7893933 5.2323323
etc.
and the output that I get printed on the screen:
0 0 5.89483e-39
0 0 5.89483e-39
0 0 5.89483e-39
Can anyone help me with that?