c++ - How do you know when an input stream has reached the last word in a line? -


i'm reading input ifstream in c++. input comes bunch of words separated tabs, i'm reading in "word1 word2 word3" stream >> w1 >> w2 >> w3; need know when i've reached final word in line, how go that? number of words variable, should even. also, last word contain \n, or \n last word?

the simplest (and usual) solution read lines using std::getline, , parse line using std::istringstream:

std::string line; while ( std::getline( std::cin, line ) ) {     std::istringstream s( line );     std::vector<std::string> words;     std::string word;     while ( s >> word ) {         words.push_back( word );     }     //  ... } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -