Reading a text file line-by-line

A proper way to read a text file line-by-line till the end is usually not clear from ifstream documentation. Let’s consider some common mistakes done by beginner C++ programmers, and a proper way to read the file.

Lines without whitespace characters

For the sake of simplicity, let’s assume that each line in the file contains no whitespace symbols.

ifstream has operator bool(), which returns true when a stream has no errors and is ready to read. Moreover, ifstream::operator >> returns a reference to the stream itself, so we can read and check for EOF (as well as for errors) in one line with very elegant syntax:

std::ifstream ifs("1.txt");
std::string s;
while (ifs >> s) {
    std::cout << s << std::endl;
}

Lines with whitespace characters

ifstream::operator >> reads the stream until any whitespace character occurs, so the above code will print the words from a line on separate lines. To read everything till the end of line, use std::getline instead of ifstream::operator >>. getline returns reference to the thread it worked with, so the same syntax is available:

while(std::getline(ifs, s)) {
    std::cout << s << std::endl;
}

Obviously, std::getline should also be used for reading a single-line file till the end.

Reading a file into a buffer at once

Finally, let’s read the file from the beginning till the end without stopping at any character, including whitespaces and newlines. If we know the exact file size or upper bound of the length is acceptable, we can resize the string and then read:

s.resize(100);
std::copy(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>(),
    s.begin());

Otherwise, we need to insert each character to the end of the string, so std::back_inserter is what we need:

std::copy(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>(),
    std::back_inserter(s));

Alternatively, it is possible to initialize a collection with stream data, using a constructor with iterator range arguments:

std::vector v(std::istreambuf_iterator<char>(ifs),
    std::istreambuf_iterator<char>());

Note that these examples are also applicable if ifs is opened as binary file:

std::ifstream ifs("1.txt", std::ios::binary);

Copying streams