c++ - cant output my whole file -


if(inputfile.is_open()){         while(!inputfile.eof()){             getline(inputfile, line);             ss << line;                 while(ss){                 ss >> key;                 cout << key << " ";                 lineset.insert(linenumber);                 concordance[key] = lineset;             }             linenumber++;         }        } 

for reason, while loop kicking out after first iteration , displays first sentence of input file. rest of code works fine, cant figure out why thinks file has ended after first iteration.

thanks

firstly should reading file without using eof , πάντα ῥεῖ notes (see here explanation):

while( getline(inputfile, line) ) {     // process line } 

note preceding if not necessary either.

the main problem , assuming ss stringstream defined earlier, comes logic:

ss << line; while(ss){     // stuff } 

the while loop here exits when ss fails. never reset ss in state. although outer loop read every line of file, of lines after first line never generate output.

instead need reset stringstream each time:

ss.clear(); ss.str(line); while (ss) {     // stuff } 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -