C++ Files and Streams6 min read

Till now, we have seen the use of iostream standard library that provides us with cin and cout methods to take input and print output respectively. Here we will learn about another library to handle files.

Files are used to store the data permanently in the storage device and a standard C++ library called fstream allows us to work with files.

fstream Library

The fstream Library provides us with three classes that help us to read and write from the files in C++. And these classes are:

  • fstream: This class generally represents a file stream. It is used to create files, write or read data from or to the files.
  • ifstream: This class represents an input stream and used to read the data from the files.
  • ofstream: This class represents an output stream and used to write the data to the files.

Open a file

We can not perform any operation on file without opening it first. If you want to perform a write operation on the file then open the file using fstream or ofstream objects. If you want to perform a read operation on the file then open the file using ifstream object.

There is a function called open() which is a member of all three classes (fstream, ifstream, and ofstream) and is used to open a file. The syntax to open a file is:

open (file_name, mode);

  • file_name: This simply indicates the name of the file to be opened.
  • mode: It defines the mode in which the file should be opened. It is opetaional. There are several modes given below.
ModeDescription
ios:: appAppend mode. The output sent to the file is appended to it.
ios::ateAt end mode. It opens the file for the output then moves the read and write control to the end of the file.
ios::inInput mode. It opens the file for reading.
ios::outOutput mode. It opens the file for writing.
ios::truncTruncate mode. If a file exists then before opening the file its contents are discarded.

Also while opening a file we can combine the above modes together using OR operator (|) such as:

Default Modes:

As stated above the mode in an open() function is optional and if we do not provide the mode then the following default mode will be assigned to the respective classes.

ClassMode
ifstreamios::in
ofstreamios::out
fstreamios::in | ios::out

Close a file

Once the program is terminated in C++, it automatically closes the opened files. However, it is recommended to practice closing a file every time you open a file.

Just like a function open() is there to open a file, the fstream, ofstream, and ifstream objects also have a close() function. The syntax is pretty simple.

void close();


Example: C++ program to open and close a file

//Output
File is created successfully.

After successful execution of the above code, you may check the directory where your c++ program file is present, there you will find a new text file with file.txt name.


Writing to a file

Below is the example of C++ to write to a file. We will write to a text file called file.txt using the stream insertion operator (<<). Here we use ofstream or fstream object instead of the cout object.

Example: C++ to write in a file.

After successful execution of the program, you can open your file.txt and you will see the following sentence inside that file.

This is Simple2Code.com.
Learn Programming Language.


Reading from a file

Below is the example of C++ to read from a file. We will from a text file called file.txt using the stream extraction operator (>>). Here we use ifstream or fstream object instead of the cin object.

Example: C++ to read from a file.

Now before running the program, you must create a file and write some text in it as for this example, we created a file called file.txt and wrote the following text in it.
This is Simple2Code.com.
Learn Programming Language.

After successful execution of the program, you will see the following output (String from opened file.)

This is Simple2Code.com.
Learn Programming Language.


Example: Read and Write in C++

Here, we will see an example where we will write some data in a text file called file.txt and then read those from the file using C++ FileStream. To write the data, we will take user input.

After Execution, a file name file.txt will be created and the following entered will be present in that file.

Writing to a file:
Enter your name: John Markson
Enter your age: 29

Reading from a file:
John Markson
29


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …