user6693361
user6693361

Reputation: 25

Reading file line by line and tokenizing lines

I have a file with multiple lines. lines contain integers separated by commas

In the following code it only parses the first line, but not the renaming lines. Any insight about I am doing wrong ?

void parseLines(std::ifstream &myfile){
  std::string line, token;
  std::stringstream ss; 
  int i;
  vector<int> r;
  while(myfile) {
    getline(myfile, line);
    ss.str("");
    ss.str(line);
    if(myfile){
      cout << "SS" << ss.str() << endl;
      while (std::getline(ss, token, ',')){
        std::cout << token << std::endl;  
      }   
    }   
  }
}

Upvotes: 0

Views: 2747

Answers (2)

NathanOliver
NathanOliver

Reputation: 180510

The issue here is the stringstream is not local to the while loop. When you read from the stringstream the first time you exhaust the stream which causes the EOF flag to be set. If you do not clear that then you will never read any more information from it even if you load more. The simplest way to get around this is to make the stringstream local to the loop body so you start off with a fresh one on each iteration and you do no have to worry about cleaning up the flags. That would make your code look like

while(getline(myfile, line)) // also moved the line reading here to control when to stop the loop
{
    std::stringstream ss(line);
    while (std::getline(ss, token, ',')){
        std::cout << token << std::endl;  
    }
}

Upvotes: 2

R Sahu
R Sahu

Reputation: 206577

Any insight about I am doing wrong?

The state of ss needs to be reset before data from the second line can be read.

Better yet, move the construction of ss inside the loop.

While at it,

  1. replace while(myfile) by while(getline(myfile, line)).
  2. Move the declaration of token inside the loop.

void parseLines(std::ifstream &myfile){
   std::string line;
   int i;
   vector<int> r;
   while( getline(myfile, line) )
   {
      std::stringstream ss(line); 
      std::string token;
      while (std::getline(ss, token, ',')){
         std::cout << token << std::endl;  
      }   
   }
}

Upvotes: 2

Related Questions