How to read a text file with C++?



In C++, you can read data from a text file using file handling features provided by the <fstream>header. This is useful when you want your program to read input stored in a file instead of typing it every time.

To do this, you use a special object called ifstream (input file stream), which helps your program open the file and read its contents line by line or word by word.

Reading a text file is helpful when:

  • You want to process saved data (like scores, settings, or logs).
  • You want to work with large inputs without typing them manually.

Reading a Text File Line by Line

You can read text from a file line by line using the getline() function. This function reads an entire line from the file and stores it in a string. It takes two parameters: the file stream object and a string variable where the line's content will be stored.

Algorithm

Here are the simple steps to read a text file line by line:

  • Include the <fstream> and <string> headers.
  • Create an ifstream object and open the file using it.
  • Check if the file was opened successfully.
  • Use a loop with the getline() function to read the file line by line.
  • Print each line.
  • Close the file.

C++ Program to Read a Text File Line by Line

This program opens a file named sample.txt and prints its content line by line to the screen.

#include<iostream> #include<fstream> #include<string> using namespace std; int main() { // Open the text file ifstream file("sample.txt"); // Check if the file was opened successfully if (!file) { cout<<"File cannot be opened!"<<endl; return 1; } string line; while (getline(file, line)) { // Print each line cout<<line<<endl; } // Close the file file.close(); return 0; } 

Assume that "sample.text" files contains the content as "This is a notes text file" and "Come back file".

If the file exists, the output will be as:

This is a notes text file Come back file 

If the file doesn't exists, the output will be as:

File cannot be opened 

Reading a Text File Word by Word

You can read text from a file word by word using the extraction operator (>>). This method reads one word at a time from the file and stores it in a string variable. It automatically skips any whitespace characters such as spaces, tabs, or newlines.

Algorithm

Here are the simple steps to read a text file word by word:

  • Include the <fstream> and <string> headers.
  • Create an ifstream object and open the file using it.
  • Check if the file was opened successfully.
  • Use a loop with the extraction operator (>>) to read the file word by word.
  • Print each word.
  • Close the file.

C++ Program to Read a Text File Word by Word

This program reads and prints each word from the file one at a time.

#include<iostream> #include<fstream> #include<string> using namespace std; int main() { // Open the text file ifstream file("sample.txt"); string word; while (file>>word) { // Print each word from the file cout<<word<<endl; } // Close the file file.close(); return 0; } 

Assume that "sample.text" files contains the content as "This is a notes text file".

If the file exists, the output will be as:

This is a notes text file 

If the file doesn't opens, the output will be as:

File cannot be opened 
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-06-13T13:18:16+05:30

75K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements