2

I have an input file of the following format:

# 1 2 3 4 5 6 7
0 0 0 1
1 0 0 1
2 0 0 1
3 0 0 1
5 0 0 1
6 0 0 1

# 0 0 2 2 4 4 5
0 0 0 1
0 1 0 1
0 2 0 1
0 3 0 1

# 9 10 11 12 13 14 15 16 17 18
0 0 0 1
0 0 1 1
0 0 2 1
0 0 3 1

Each line preceded by a # must be read into its own vector. The entries in between these vectors represent matrices that also must be read into their own matrix.

So from the input file above, what I want to end up having is the following:

knot1 = {1 2 3 4 5 6 7}
cp1= { {0,0,0,1} {1,0,0,1} {2,0,0,1} {3,0,0,1} {5,0,0,1} {6,0,0,1} } 

knot2 = {0 0 2 2 4 4 5}
cp2= {{...} {...} {...} {...} }

knot3 = {9 10 11 12 13 14 15 16 17 18}
cp3= {{...} {...} {...} {...} }

Note, each vector is not necessarily the same size! Same goes for the matrices. Also, the number of #vectors and matrices can vary as well.

Here is what I have so far:

    ifstream file;
    file.open(filename.c_str());

    if(file.fail()){
        cout << "Cannot open " << filename << endl;
    }

    int curr_line = 0;
    vector<int> knot_locations;    //stores the locations of the #vectors

    while(!file.eof()){       //loops over input file checking to see where the #vectors are
        curr_line++;
        string line;
        getline(file,line);
        if(line[0]=='#'){
            knot_locations.push_back(curr_line);
        }
    }

    for(int i=0; i < knot_locations.size(); i++){
        file.seekg(std::ios::beg);
        for(int i=0; i < knot_locations[i] - 1; ++i){     // this loop skips to the line that contains the #vectors.
            file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        }
    }

so now that I am at the line containing the vector, how can I read in JUST that SINGLE line into a vector?! I'm not sure how to turn a string into a vector of floats. Also, since I know all the locations of the vectors, I can read everything else between into the matrices. But again, same problem. I am not sure how to go about actually reading these into a numeric array/vector given a line (string)

    file.close();

Probably better ways of doing this. Any ideas on how to go about this problem? The key is to be able to read all the vectors marked with a # into their own vector. There can be anywhere between 1-3 of these vectors. And in between each of these vectors is a matrix of unknown rows/columns that also need to be read into their own matrix. What I have above just locates the # marked vectors. Need help on how to read a string line into a numeric array OR a recommendation on a different way to go about this.

Thank you.

Mike James Johnson
  • 684
  • 2
  • 7
  • 28
  • 2
    1st issue I spot: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Jul 08 '16 at 23:31
  • 1
    Counting the vectors and then seeking back to read their contents seems a roundabout way of doing it. Why not initialise the vectors as you go? When you get a `#` add a new vector to a list, and when you get a line of data add it to the current vector. – Jonathan Potter Jul 08 '16 at 23:38
  • @JonathanPotter my main problem here is How can I read a single line into a numeric array. – Mike James Johnson Jul 08 '16 at 23:47
  • 4
    Thousands of examples of that on SO, it seems to be a common homework problem. E.g. http://stackoverflow.com/questions/8377660/how-to-cin-to-a-vector. – Jonathan Potter Jul 08 '16 at 23:49

0 Answers0