In this article, I have explained how you can perfom file handling operatins like writing to file or reading from file in C++ using fstream, ofstream and ifstream. Using file handling in C++ we write to or read data from, files stored on hard disk.

What is File?

Before we proceed further, we will like to introduce you about File, so file is a logical collection of records where each record consists of number of items known as fields. We can check file-type by it's extension, for example : .txt(text file), .cpp(c++ source file), .exe(executable file), .pdf(portable document file) and there are many more file-types..

Now, the records in files can be arranged in three ways, Ascending or Descending Order, Alphbetically and last one is Chronologically (Basically records is arranged in order of occurence).

Files and Streams in C++

In C++, file handling is done using streams, a stream is a data flow from a source to a sink. The sources and sinks can be any of the input/output devices or files.

In C++, fstream is used to read or write from files.

So it can be divided into three classes, ifstream, ofstream and fstream.

  • ifstream (ios::in): used to read data from files
  • ofstream (ios::out): used to write to file and open files in C++
  • fstream (ios::in | ios::out): used to both read and write data from/to files

Functions used to handle files in C++

You can use the below functions to open, read, write or close functions in C++.

  • open() - to create a file
  • get()- reads a characters from currently opened file
  • put() - write a character to currently opened file
  • read()- to read data from file
  • write() - used to write data into file.
  • close()- to close a file
  • getline()- it is used to read a string or a line from an input stream.

Writing to a File

  • The insertion operator (<<) is used to write information in a file.
  • The ofstream or fstream object is used instead of cout object.

Reading from a File

  • The extraction operator (>>) is used to read information from a file into your program.
  • The ifstream or fstream object is used instead of cin object.

File Operation Modes

We can have following modes to open file and read from it or write into it

  • ios :: out - open the file for write only.
  • ios :: in - open the file for read only.
  • ios :: app - open the file for appending data to end-of-file.
  • ios :: ate - take cursor to the end of the file when it is opened.
  • ios :: binary - when using this mode, file is opened in binary mode.
  • ios :: trunc - If the file is already exists, using this mode the file contents will be truncated before opening the file.

Example of Reading and writing to file in C++

#include<iostream>//Required for input & output: 
#include<fstream>//Required for File function:
#include <string> //required for string operations

using namespace std;

int main()
	/*Create New file: in just Write Mode:*/
	fstream myFile;
	char text[200];
	char output[200];
	cout << "WRITING PURPOSE:" << endl;"E:\CppFile.txt", ios::out); //open file in writing mode:

	if (!myFile) //error checker: if file did'nt open:
		cout << "Error while Opening File: " << endl;
	else {

		cout << "Write a line to be written on file." << endl;
		cin.getline(text, sizeof(text)); //get text from User

		// Writing text on file
		myFile << text << endl;


	myFile.close(); //close the file:
	if (myFile.is_open() == NULL) //error checker, if file still open:
		cout << "File has been Closed" << endl;
		cout << " File still Open: " << endl;

	cout << endl; //line break

	//read from file
	//code starts here
	cout << "Now we will read from file." << endl;


	ifstream ifile;"E:\CppFile.txt", ios::in);  // open a file in read mode.

	//ifile >> output;   //read only one word
	//cout << "Selects only first word = " << output << endl;

	cout << "Get Complete line using getline " << endl;
	string tp; //string to save line
	while (getline(ifile, tp)) { //read data from file object and put it into string.
		cout << tp; //print the data of the string
	cout << endl;

	ifile.close(); //close file after reading




You can also verify the file, it must have output like below


I have created above project in Visual Studio 2017 Community version, you can also open Visual Studio 2017 Community or other version and create new project in C++ by navigating to Visual C++ -> Windows Desktop (from left pane) and Console App (from right-pane), as shown below, name the project and click "ok"


From solution explorer, click "Source Files"-> "CppHandling.Cpp" file to open it and use above code.

You may also like to read:

Switch case in C++ with example

Infix to Postfix in C using Stack

File program in C (Open, Read and Write)

C Program for matrix multiplication using Pointers and Without Pointers

File Handling in C#