How to Read Int From a File in C++

  1. Use while Loop and >> Operator to Read Int From File in C++
  2. Use while Loop and >> Operator Combined With push_back Method to Read Int From File
  3. Don’t Use while Loop and eof() Method to Read Int From File

This article will explain several C++ methods on how to read int data from a file.

In the following example programs, we assume a text file is named input.txt, which contains space-separated integer numbers on multiple lines. Note that each sample code checks if this filename gets associated with the actual file stream and prints the corresponding failure message in case of error.

  • Content of input.txt:
123 178 1289 39
90 89 267 909 23
154 377 34 974 322

Use while Loop and >> Operator to Read Int From File in C++

This method uses the while loop to iterate the process until the EOF (end of the file) is reached and store each integer in the number variable. Then, we can print each number to console in the loop body.

#include <iostream>
#include <fstream>

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream;

int main()
{
    string filename("input.txt");
    int number;

    ifstream input_file(filename);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        return EXIT_FAILURE;
    }

    while (input_file >> number) {
        cout << number << "; ";
    }
    cout << endl;
    input_file.close();

    return EXIT_SUCCESS;
}

Output:

123; 178; 1289; 39; 90; 89; 267; 909; 23; 154; 377; 34; 974; 322;

Use while Loop and >> Operator Combined With push_back Method to Read Int From File

As another alternative, we can get each integer from the file, store it in the number variable like in the previous example, and then push them to the int vector in each iteration. Notice that this scenario includes another for loop to imitate the more practical system where the elements of the stored vector numbers need to be manipulated.

#include <iostream>
#include <fstream>
#include <vector>

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::vector;

int main()
{
    string filename("input.txt");
    vector<int> numbers;
    int number;

    ifstream input_file(filename);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        return EXIT_FAILURE;
    }

    while (input_file >> number) {
        numbers.push_back(number);
    }

    for (const auto &i : numbers) {
        cout << i << "; ";
    }
    cout << endl;
    input_file.close();

    return EXIT_SUCCESS;
}

Output:

123; 178; 1289; 39; 90; 89; 267; 909; 23; 154; 377; 34; 974; 322;

Don’t Use while Loop and eof() Method to Read Int From File

One might consider using the eof() member function as a while loop condition to approach the same problem. Unfortunately, that may lead to an extra iteration. Since the function eof() only returns true if the eofbit is set, it might lead to the iteration where uninitialized is modified. This scenario is demonstrated by the code sample below:

#include <iostream>
#include <fstream>
#include <vector>

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::vector;

int main()
{
    string filename("input.txt");
    vector<int> numbers;

    ifstream input_file(filename);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        return EXIT_FAILURE;
    }

    while (!input_file.eof()) {
        int tmp;
        input_file >> tmp;
        numbers.push_back(tmp);
    }

    for (const auto &i : numbers) {
        cout << i << "; ";
    }
    cout << endl;
    input_file.close();

    return EXIT_SUCCESS;
}

Output:

123; 178; 1289; 39; 90; 89; 267; 909; 23; 154; 377; 34; 974; 322;

Related Article - C++ File

  • How to Read File Char by Char in C++
  • How to Read a File Line by Line in C++