Open In App

How to Read From a File in C++?

Last Updated : 12 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:

  1. Text File: Files that contains data in the form of text (characters).
  2. Binary File: Files that contains data in raw binary form.

The method of reading data from these files also differs from one another. Let's see how to read each type of files.

Read From a Text File

To read the content of this text file in C++, we have to first create an input file stream std::ifstream to the file in default flags. After that, we can use any input function, such as std::getline() or >> operator to read the textual data and store it in the string variable. We generally prefer getline() as it reads till the newline, or any specified character is found.

Example

Assume that your text file has name textFile.txt and contains the following data:

readFile
textFile.txt

Then, we can read the data of this file as shown:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Open the text file named 
    // "textFile.txt"
    ifstream f("textFile.txt");

    // Check if the file is 
    // successfully opened
    if (!f.is_open()) {
        cerr << "Error opening the file!";
        return 1;
    }
    string s;

    // Read each line of the file, store
    // it in string s and print it to the
    // standard output stream 
    while (getline(f, s))
        cout << s << endl;

    // Close the file
    f.close();
    return 0;
}


Output

Hey Geek!
Welcome to GfG.
Happy Coding.

Read From a Binary File

Binary files in C++ are used to store data in the binary form 0s and 1s. To read the data fo binary file, we need to open the input file stream (ifstream) object with the std::ios::binary flag. The getline() function cannot read the data from the binary files so we use the specialized function read() of ifstream class that reads the given block of data from the file stream and return it as a character array which can be converted to the required type using reinterprit_cast.

Syntax of read()

C++
stream.read(buffer, size)

where,

  • stream: A valid ifstream object to binary file.
  • buffer: Pointer to the character array where the read data is to be stored.
  • size: Number of bytes to read.

Example

Consider the binary file contains the following data:

writeBinary
fileBin.bin

We can read this file as shown:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str;

    // Open the binary file for reading
    ifstream file("fileBin.bin", ios::binary);
    if (!file) {
        cerr << "Error opening file for reading.";
        return 1;
    }

    // Read the length of the string (size)
    // from the file
    size_t strLength;
    file.read(reinterpret_cast<char*>(&strLength),
        sizeof(strLength));

    // Allocate memory for the string 
    // and read the data
    // +1 for the null-terminator
    char* buffer = new char[strLength + 1];  
    file.read(buffer, strLength);

    // Null-terminate the string
    buffer[strLength] = '\0';

    // Convert the buffer to a string
    str = buffer;

    // Print file data
    cout << "File Data: " << str;
    delete[] buffer;
    
    // Close file
    file.close();
    return 0;
}


Output

File Data: Welcome to GeeksForGeeks

One thing we can infer from here is that you need to know about the format or type of the contents stored in the binary file to successfully read it. It is not true for text file though, as we can read everything present in it as character.


Next Article
Practice Tags :

Similar Reads