C++ Files And Streams - C++ Programming Tutorial
C++ Course / File Handling / C++ Files And Streams

C++ Files And Streams

BLUF: Mastering C++ Files And Streams is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Files And Streams

C++ is renowned for its efficiency. Learn how C++ Files And Streams enables low-level control and high-performance computing in the tutorial below.

In C++ development, the iostream standard library is utilized, offering the cin and cout functions for input and output operations correspondingly.

To perform reading and writing operations on files, we utilize the fstream library provided by C++. Now, let's explore the data types defined within the fstream library:

Data Type Description
fstream It is used to create files, write information to files, and read information from files.
ifstream It is used to read information from files.
ofstream It is used to create files and write information to the files.

C++ FileStream example: writing to a file

Let's explore a basic illustration of how to write to a text file named testout.txt using C++ FileStream programming.

Example

Example

#include <iostream>

#include <fstream>

using namespace std;

int main () {

  ofstream filestream("testout.txt");

  if (filestream.is_open())

  {

    filestream << "Welcome to javaCppTutorial.\n";

    filestream << "C++ Tutorial.\n";

    filestream.close();

  }

  else cout <<"File opening is fail.";

  return 0;

}

Output:

Output

The content of a text filetestout.txtis set with the data:

Welcome to javaCppTutorial.

C++ Tutorial.

C++ FileStream example: reading from a file

Let's explore a basic illustration of how to read from a text file named testout.txt using C++ FileStream programming.

Example

Example

#include <iostream>

#include <fstream>

using namespace std;

int main () {

  string srg;

  ifstream filestream("testout.txt");

  if (filestream.is_open())

  {

    while ( getline (filestream,srg) )

    {

      cout << srg <<endl;

    }

    filestream.close();

  }

  else {

      cout << "File opening is fail."<<endl; 

    }

  return 0;

}

Note: Before running the code a text file named as "testout.txt" is need to be created and the content of a text file is given below: Welcome to javaCppTutorial. C++ Tutorial.

Output:

Output

Welcome to javaCppTutorial.

C++ Tutorial.

C++ Read and Write Example

Let's explore a basic illustration of writing information to a text file named testout.txt and subsequently retrieving the data from the file utilizing C++ FileStream coding.

Example

Example

#include <fstream>

#include <iostream>

using namespace std;

int main () {

   char input[75];

   ofstream os;

   os.open("testout.txt");

   cout <<"Writing to a text file:" << endl;

   cout << "Please Enter your name: "; 

   cin.getline(input, 100);

   os << input << endl;

   cout << "Please Enter your age: "; 

   cin >> input;

   cin.ignore();

   os << input << endl;

   os.close();

   ifstream is; 

   string line;

   is.open("testout.txt"); 

   cout << "Reading from a text file:" << endl; 

   while (getline (is,line))

   {

   cout << line << endl;

   }	

   is.close();

   return 0;

}

Output:

Output

Writing to a text file:  

 Please Enter your name: Nakul Jain    

Please Enter your age: 22  

 Reading from a text file:   Nakul Jain  

 22

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience