Question

The file format looks like:

A 1 2 3
B 1 2 3
...

And I want to read them in a vector of struct Observation that looks like:

struct Observation {
    string category;
    vector<int> variables;
};

How can I read them? I've searched for some solutions here, and tried:

int main() {
    ifstream infile("data.txt");
    string line;
    string category;
    vector<Observation> obs;
    int i = 0;
    while(getline(infile, line)) {
        istringstream iss(line);
        iss >> category;
        int j = 0;
        int value;
        while(iss >> value) {
            obs[i].variables[j] = value;
            j++;
        }
        i++;
    }
}

but it says: error: variable ‘std::istringstream iss’ has initializer but incomplete type my compilier is g++ 4.6.3 on Ubuntu 12.04, complie with command "g++ -std=c++0x io.cpp -g -o io", please help me with that, thanks!

Was it helpful?

Solution

Try including <sstream> and also change the way you add to the vector:

while(iss >> value) {
    obs[i].variables.push_back(value);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top