Read a File Line by Line in C++

Lasha Khintibidze Dec 10, 2020 Oct 17, 2020
  1. Use std::getline() Function to Read a File Line by Line
  2. Use C Library getline() Function to Read a File Line by Line
Read a File Line by Line in C++

This article will introduces how to read a file line by line in C++.

Use std::getline() Function to Read a File Line by Line

The getline() function is the preferred way of reading a file line by line in C++. The function reads characters from the input stream until the delimiter char is encountered and then stores them in a string. The delimiter is passed as the third optional parameter, and by default, it’s assumed to be a new line character \n.

Since the getline method returns a nonzero value if the data can be read from the stream, we can put it as a while loop condition to continue retrieving lines from a file until the end of it is reached. Note that it’s good practice to verify if the input stream has an associated file with the is_open method.

#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<string> lines;
    string line;

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

    while (getline(input_file, line)){
        lines.push_back(line);
    }

    for (const auto &i : lines)
        cout << i << endl;

    input_file.close();
    return EXIT_SUCCESS;
}

Use C Library getline() Function to Read a File Line by Line

The getline function can be used similarly to loop through the file line by line and store extracted lines in a character string variable. The main difference is that we should open a file stream using the fopen function, which returns the FILE* object, later to be passed as the third parameter.

Notice that getline function stores at char pointer, which is set to nullptr. This is valid because the function itself allocates the memory dynamically for retrieved characters. Thus, the char pointer should be explicitly freed by a programmer, even if the getline call failed.

Also, it’s important to set the second parameter of type size_t to 0 before calling the function. You can see the detailed manual of the getline function here.

#include <iostream>
#include <vector>

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

int main()
{
    string filename("input.txt");
    vector<string> lines;
    char* c_line = nullptr;
    size_t len = 0;

    FILE* input_file = fopen(filename.c_str(), "r");
    if (input_file == nullptr)
        return EXIT_FAILURE;

    while ((getline(&c_line, &len, input_file)) != -1) {
        lines.push_back(line.assign(c_line));
    }
    for (const auto &i : lines) {
        cout << i;
    }
    cout << endl;
    fclose(input_file);
    free(c_line);

    return EXIT_SUCCESS;
}

Related Article - C++ File