C++20 sections · 1024 units
Open in Course

Reading Line by Line

Handling full lines

getline(file, str) reads an entire line from file into string str, including spaces. It stops at the newline character (consuming it but not storing it). This beats >> for reading sentences or formatted text.

Common pattern: while(getline(file, line)) { process(line); }. The loop continues until reaching end-of-file. Each iteration gives you one complete line to parse or display. You can specify a custom delimiter: getline(file, str, ',') reads until a comma instead of newline.

This parses CSV files where commas separate values. The delimiter is consumed but not included in str.