Word by Word printing...

Howdi,

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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 ?

Thanks in advance,
Sincerely,
Jose
The >> operator stops reading string input at whitespace, NUL, and EOF.

See http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/ (read the section titled Parameters for the str name).

Hope this helps.
Topic archived. No new replies allowed.