mercredi 29 avril 2015

Read from file line by line and parse to vector a lot of ints in c++

I have seen a lot of similar posts regarding very similar cases but my case is a bit different. I'm a newbie to c++, so any help would be appreciated.

I have a large file full of lines full of integers. Each number is separated by blank spaces. I need diferent lines to stay seperate, i don't want to read all the file on one go. I want to read line by line and parse each line in to a vector of integers. The code I've got is this:

int main () {
  string line;
  ifstream myfile; ("numbers.txt");
  vector<int> vec1;
  int const2=0;
  int a;
  while ( getline (myfile,line) ){ // I understand that this reads line 
                                   // by line and stores the string to "line"
    while (line >> a){  // this part is the one i can't get right, i 
                        // want to push_back every int from
                        // the string to vec1 but doesn't work
      // More stuff
    // more stuff
  return 0;

Aucun commentaire:

Enregistrer un commentaire