C++ Reading from a file


There are several ways to read data from a file.

If you know how the data is formatted, you can use the stream extraction operator (>>). Let's assume you have a file named foo.txt which contains the following data:

John Doe 25 4 6 1987
Jane Doe 15 5 24 1976

Then you can use the following code to read that data from the file:

// Define variables.
std::ifstream is("foo.txt");
std::string firstname, lastname;
int age, bmonth, bday, byear;

// Extract firstname, lastname, age, bday month, bday day, and bday year in that order.
// Note: '>>' returns false if it reached EOF (end of file) or if the input data doesn't
// correspond to the type of the input variable (for example, the string "foo" can't be
// extracted into an 'int' variable).
while (is >> firstname >> lastname >> age >> bmonth >> bday >> byear)
    // Process the data that has been read.

The stream extraction operator >> extracts every character and stops if it finds a character that can't be stored or if it is a special character:

  • For string types, the operator stops at a whitespace () or at a newline (\n).
  • For numbers, the operator stops at a non-number character.

This means that the following version of the file foo.txt will also be successfully read by the previous code:

Doe 25
4 6 1987

15 5

The stream extraction operator >> always returns the stream given to it. Therefore, multiple operators can be chained together in order to read data consecutively. However, a stream can also be used as a Boolean expression (as shown in the while loop in the previous code). This is because the stream classes have a conversion operator for the type bool. This bool() operator will return true as long as the stream has no errors. If a stream goes into an error state (for example, because no more data can be extracted), then the bool() operator will return false. Therefore, the while loop in the previous code will be exited after the input file has been read to its end.

If you wish to read an entire file as a string, you may use the following code:

// Opens 'foo.txt'.
std::ifstream is("foo.txt");
std::string whole_file;

// Sets position to the end of the file.
is.seekg(0, std::ios::end);

// Reserves memory for the file.

// Sets position to the start of the file.
is.seekg(0, std::ios::beg);

// Sets contents of 'whole_file' to all characters in the file.

This code reserves space for the string in order to cut down on unneeded memory allocations.

If you want to read a file line by line, you can use the function getline():

std::ifstream is("foo.txt");   

// The function getline returns false if there are no more lines.
for (std::string str; std::getline(is, str);) {
    // Process the line that has been read.

If you want to read a fixed number of characters, you can use the stream's member function read():

std::ifstream is("foo.txt");
char str[4];

// Read 4 characters from the file.
is.read(str, 4);

After executing a read command, you should always check if the error state flag failbit has been set, as it indicates whether the operation failed or not. This can be done by calling the file stream's member function fail():

is.read(str, 4); // This operation might fail for any reason.

if (is.fail())
    // Failed to read!