C++20 sections · 1024 units
Open in Course

Problem - Word Counter

File input basics

You'll read text from a file and count how many words it contains. This teaches you the core pattern of opening a file, reading its contents, and processing the data line by line or word by word.

File I/O in C++ uses stream objects similar to cin and cout. The ifstream class reads from files, and ofstream writes to files.

This problem demonstrates reading until end-of-file. When the file is exhausted, the loop ends.