I have a Plain Text matrix file (called q.txt):
1 0 0
0 2 0
0 1 1
0 0 1
1 1 1
which I would like to read into a matrix, which is a vector containing vectors (which contain doubles).
My current codes yields the error "newReader.cpp:22: error: variable ‘std::istringstream reader’ has initializer but incomplete type" Here is the code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
typedef vector<double> Vec; //Vector
typedef vector<Vec> Mat; //Matrix
int main(void){
Mat data;
string line;
ifstream myfile ("q.txt");
if( myfile.is_open() ){ while( myfile.good() ){
while(!std::getline(myfile, line, '\n').eof()) {
istringstream reader(line);
Vec lineData;
string::const_iterator i = line.begin();
while(!reader.eof()) {
double val;
reader << val;
if(reader.fail()) break;
lineData.push_back(val);
}
data.push_back(lineData);
}
}}
else{ cout<< "Unable to open file." <<endl; }
return 0;
}