C++20 sections · 1024 units
Open in Course

Reading Word by Word

Processing individual tokens

To read words (whitespace-separated tokens), use >> in a loop: while(file >> word) { process(word); }. The operator skips spaces, tabs, and newlines automatically, giving you one word at a time.

This pattern works for counting words, finding specific terms, or building word frequency maps. The loop stops when reaching end-of-file or encountering a read error. Punctuation stays attached to words: "hello," reads as a single token including the comma.

If you need pure words, strip punctuation manually using string methods or regular expressions.