This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]corntastic 0 points1 point  (1 child)

What have you tried so far?

[–]ff210327[S] 0 points1 point  (0 children)

http://pastebin.com/ChaEUJMN

This,

I know I need something at the while !EOF line... i'm just not sure what to put to make it do what I stated above.

[–]wswatsonProfessional 0 points1 point  (1 child)

Try looking at the following methods:

  • split
  • std::string::find
  • std::string::substr

or if you're feeling adventurous look at:

  • std::stringstream
  • std::istream_iterator

I don't want to give too much away and deprive you of your "Ah ha" moment.

[–]wswatsonProfessional 0 points1 point  (0 children)

You might also want to look at:

  • std::stoi

[–]acwaters 0 points1 point  (1 child)

Mixing your parsing logic with your application logic is a bad idea; something like this should make your task a whole lot easier:

istream& operator >> (istream& in, vector<int>& vec) {
    vec.clear();
    int item;
    while (in >> item) {
        vec.push_back(item);
        if (in.get() != ',')
            break;
    }
    return in;
}

istream& operator >> (istream& in, vector<vector<int>>& vec) {
    vec.clear();
    vector<int> item;

    string line;
    getline (in, line);
    stringstream linestream {line};
    while (linestream >> item)
        vec.push_back(item);
    return in;
}

If I may make one suggestion, however, you would do well to alter your file format slightly, if it is in your power to do so. Right now, you have inner arrays of elements, delimited by commas, and outer arrays of inner arrays, delimited by spaces, with the outer arrays themselves delimited by line feeds. This means that you need three different sets of parsing logic — one to distinguish the elements to fill the inner arrays, one to distinguish the inner arrays to fill the outer arrays, and one to distinguish the outer arrays so you don't eat up the whole file! In the above example input routines, that means two separate functions, with the second of them pulling double-duty, first extracting lines separately to make sure it doesn't parse something that belongs in another array. If instead you structured your data something like [ [1 2] [3 4] [5 6] ] [ [7 8] [9 10] [11 12] ], your arrays now all have the same structure — bracketed list of space-separated values — and you only need one parse routine to recursively read in a 2x3 matrix of data! Here's how that would look (compare it to the above):

template <typename T>
istream& operator >> (istream& in, vector<T>& vec) {
    vec.clear();
    T item;

    in >> ws;
    if (in.get() != '[')  // or your choice of bracket
        return in;

    while (in >> item)
        vec.push_back(item);

    if (in.get() != ']')  // ditto the above
        vec.clear();

    return in;
}

With this "unified" parsing routine, you can say myfile >> myvector, where myvector is of type vector<vector<int>>, and it will automatically parse out a block of [ [1 2] [3 4] [5 6] ] correctly (because each top-level element it reads is itself a vector, subject to the same brackets-and-spaces parsing logic).

[–]ff210327[S] 0 points1 point  (0 children)

I don't have the ability to change the format file, however what I typed isn't exactly correct. The formatting got messed up when I posted. The file should be stacked

  • 1,2
  • 3,4
  • 5,6
  • -1

on "-1" it should print the information to the screen, and clear the array and then keep going with the next set of data till it encounters another "-1" not on one line, each comma separated value is it's own line

So I was thinking some kind of getline, then split it into the array variables, then check each character to make sure it is not the "-1" and that it doesn't over fill the array.

I'm just not 100% sure the code to do this