This program refuses to take foom as input, yet if I were to put "matrix.txt" into the function instead of foom, it would work fine.

#include <fstream>
#include <iterator>
#include <sstream>
#include <vector>
#include <string> // needed?

using namespace std;

typedef vector<double> Vec;
typedef vector<Vec> Mat;

Mat readMatrix(string foom){
Mat x;
    ifstream in(foom);
    if (in) {
        for (string line; getline(in, line);) {
            istringstream iss(line);
 
            x.push_back(Vec());
            copy(istream_iterator<double>(iss), 
                istream_iterator<double>(), 
                back_inserter(x.back()));
        }
    }
return x;
}

Recommended Answers

All 3 Replies

You want ifstream in(foom.c_str());

Thanks so much! Quick and painless! Also, it seems that #include <string> is not needed for the program to run. Is <string> only for some advanced string functions?

Thanks so much! Quick and painless! Also, it seems that #include <string> is not needed for the program to run. Is <string> only for some advanced string functions?

include <string> whenever you need to use std::string.

Be a part of the DaniWeb community

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