Coders Packet

File Handling in C++

By Pratiti Prashant Pande

This module discusses the concepts, various modes, and functions used in File handling in C++

When we write a program in C++, compile and run it the data is in the memory but when it ends or we shut down the computer it gets lost. That means it is not stored permanently.
In today's world storing data permanently has become vital and that is why we need files to store our data permanently.

So this module discusses file handling in C++, the set of classes that defines file handling methods, and some functions and syntaxes that are used in file handling in C++
Let's start by understanding what is meant by File and Streams
Files, as we know, is used to store data and streams refer to series of bytes

There are 2 types of streams: input and output
The input stream extracts (reads) data from the file and the output stream inserts (writes) data to file
This is explained in the following figure

As we know to use certain functions in our program we need to include a particular header file for it. Similarly, for using the file handling functions we have to use the header file . This header file has 3 classes
ifstream: used to read the stream of objects from a file. 
ofstream: used to write stream of objects to a file. 
fstream: Provides support for simultaneous input and output operations

The above classes are derived from fstreambase and the corresponding iostream.h class. They are defined to manage the disk files and declared in fstream.h

A) To open a file: 
There are two ways to open a file : 1) By using constructor function of the class
Syntax--> For performing write function
ofstream outf ("result");
where ,
ofstream: opens file in write mode
outf: object
"result " : file_name
Similarly for performing read function
Syntax : ifstream inf ("result");

2) Using member function open() of the class
Syntax: For performing write function
ofstream outf;"file_name");

The difference between the above two ways is that with the constructor function the stream is used to link to one file only while in open() function same stream is used to link multiple files

B) To close a file
Syntax--> outf.close();  //to close a file which is in write mode                                                                                                                                Syntax--> inf.close();    //to close a file which is in read mode

We have seen that we can use the open() function to open a file. This open() function can be used to pass the following parameters
Syntax--> open( "filename", file_mode);

ios::in --> opens a file in reading mode only
ios::out --> open a file for write mode only
ios::app--> Append content only at the end of file
ios::ate--> Append content at end of file as well as user can edit previous content also
ios::binary--> Creates Binary file
ios::nocreate--> Does not create a new file and file cannot be opened if it has been not created earlier
ios::noreplace--> Open fails if the file already exists
ios::trunc--> Deletes content of file if it exists and new content could be added

To use more than one parameters, we use bitwise 'or' operator
Example " Hello", ios::ate | ios::noreplace);

C) write():
Syntax -->
outf.write((char*) & variable, sizeof(variable));
This function is used with object of ofstream and is used to store data in binary mode
Here all the variables are first of all converted into (char*) ie pointer to character and second parameter is number of byte required to store given variable

Also this function can be used with cout
Syntax--> cout.write(variable,size of variables)
The main difference between cout.write and cout is that in cout.write we can control the length of characters while this is not available in cout

D) read()
This function is used with object of ifstream and is used to read data in binary mode
Syntax -->*) & variable, sizeof(variable));

The other types of functions are
get(): This function is used to read a single character from a file ,specified by object of stream
Syntax--> inf.get(character_variable);

put(): This function is used to store a single character into a file, specified by the object of the stream
Syntax--> outf.get(character_variable);

getline(): this is used with cin object
Syntax--> cin.getline(variable,size);
Note: cin does not read complete string having spaces the string terminates as we give space. While cin.getline() reads the whole string including spaces and gets terminated when we press ENTER button for starting a new line

So these are some basic functions and syntaxes in the file handling function 

Below is the sample output of the program 

In this, we can see the first line which gives us the path where our text file is created. On reaching the path we must find our required file. We need to right-click on it and open it with say Notepad and the content that we have stored in our file would appear in the Notepad


Download Complete Code


No comments yet