×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

Read data from a text file using C++

In this tutorial, we will learn how to read data from a text file using C++ program? By IncludeHelp Last updated : October 30, 2023

Problem statement

Given a text file (or, create a text file, then insert data in it), the task is to read data from this text file using C++. For example, there is a file named "file.txt" that has the following data:

Hello, world!
Output would be:
Hello, world!

Reading data from a text file in C++

  • Use the fstream class to read data from the file, create an object fs of this class.
  • Now, open the file using the open() function with iso:in mode.
  • If file opened successfully, then read the whole data line by line using the getline() function.
  • Assign this value to a string variable str.
  • Print the string.
  • And, then close the file.

C++ program to read data from a text file

In this example, we will have a file and read data from it.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
  // Create an object of fstream class
  fstream fs;

  // open file
  fs.open("file.txt", ios::in);
  
  // check whether file is opened or not
  if (fs.is_open()) {
    string str;
    //read data from file object
    // and assign the value into string.
    while (getline(fs, str)) {
      //print the data of the string
      cout << str << "\n";
    }
  
    //Now, close the file object.
    fs.close();
    
    return 0;
  }
}

File

file.txt contains the following data:

Hello, world!

Output

The output of the above program is:

Hello, world!

Write data and then read data from the text file

If file is not created, you can also create the file using the code and then write some data into in it.

Example

In the below example, we are creating a text file "file.txt" first, inserting two lines in it and then reading the data from it.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
  // Create an object of fstream class
  fstream fs;

  // Create file and write data in it
  
  // iopen file in write mode ios::out
  fs.open("file.txt", ios::out);
  
  // check whether file is created or not
  if (fs.is_open()) {
    // Now write some data int it 
    // Note: Here, you can also take input from the user
    fs << "Hello, world!\n";
    fs << "Welcome here.\n";
  
    //close the file object
    fs.close();
  }

  //Now, read data from the text file
  // open file
  fs.open("file.txt", ios::in);

  // check whether file is opened or not
  if (fs.is_open()) {
    string str;
    //read data from file object
    // and assign the value into string.
    while (getline(fs, str)) {
      //print the data of the string
      cout << str << "\n";
    }

    //Now, close the file object.
    fs.close();
    
    return 0;
  }
}

Output

Hello, world!
Welcome here.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.