I have this small program that reads a line from a file at a time, then, it reads one word at a time from the obtained line. Because I copied this piece of code, I would like to have this last part explained...
#include <iostream>
#include <fstream>
#include <sstream>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() {
std::ifstream file("textfile.txt");
if(file) {
string line, word;
while(getline(file, line)) {
//cout << line << endl;
std::istringstream is(line);
//how does the following while works?
while(is >> word) {
cout << word << "\t";
}
}
}
file.close();
file.clear();
return 0;
}
I don't understand how the >> operator reads ONE word assigns it to the word string and moves to the next word. I know it has at least something to do with the space between words, but it's not explicit to me. Could somebody please explain it to me ?