In every programming language, File handling plays an essential role. When we create any program, the input is taken from standard input devices and output is from monitor i.e. standard output device where cin and cout are used. But these all are temporary because when we exit from a program, data never save as it is on temporary memory. Whenever we want to store data permanently in the hard disk in the form of a file, file handling is used.
OVERVIEW OF FILE HANDLING
We store our program's data in a variable
a) Suppose we want to store some data of a program written in C++ language.
b) We start executing the specific program
c) After execution, it goes to ram where it gets some memory and the data get stored in some variables but they are temporary.
d) After termination, the variable vanishes and destroys stored data also.
Before termination of the program when we want to store it permanently in storage memory or secondary memory in the form of a file, this whole process is known as file handling.
Stream act as an interface between keyboard and program and contain a sequence of a byte. Suppose we enter input i.e. 15 with the help of keyboard it goes to the program which is in a temporary memory and after that, it transfers from temporary memory to monitor where it gets to write or read. When we are getting input from the keyboard, cin is used and in the same way, when we want to get the output we use cout which means cin and cout are input and output streams.
* Input stream receives data from the standard and input device where the input stream act as an interface between programs and input device and helps to read. The output stream is used for writing on the monitor.
Input stream and output stream roles in the form of Diagram
To implement file processing in C++ programming language, insertion of header files such as <iostream> and <fstream> in the source code is mandatory.
In C++, files are handled mainly with the three classes i.e. fstream, ifstream, and ofstream.
a) Ifstream contains the methods that are used to read from the files. Where, the functions such as get( ), getline( ), read( ), seekg( ) and tellg( ) can be inherits from ifstream class
b) Ofsteam contains the methods that are used to write on file where put( ), write( ), seekp( ) and tellp( ) can be inherits from ofstream
c) fstream contains the methods that are used to perform both write and read operations on/from the file. Seekg, seekp, tellg, open and close are the functions that can be inherited from fstream
These classes are the part of istream and ostream, where cin and cout are their objects. File streams are used in the similar method like cin and cout objects with the only difference that there required a physical file association with file streams.
The file contains information about a collection of records under a specific name. Each record is known as a field.
The arrangement of files can be done in the following three ways
Descending /Ascending order
These are the files with a format that stores data and information in ASCII characters. Every line of text is terminated with delimiter character that is also known as EOL (End of Line) character.
Create a file
# A suitable name for the File.
# Data type and structure
# Opening Method
A suitable name for the File
When we want to create any file, a suitable name is required.
codingtag.doc which consist of the file name and its extension
|File Created||File Name||Extension|
Datatype and Structure
This part includes an opening and closing file. To perform any read-write operation, there is a need for file stream
To open any file
# Create a file and then link it to file name.
# For creating file stream, header files such as fstream.h are used.
# For defining classes, ifstream, ofstream and fstream are used.
Objective of file
As we already known that we can perform read and write operations on the file. Classes can be used according to the purpose of files
Methods of opening a file
There are two methods for opening any file
# With the usage of Constructor function of the class.
# With the usage of Member function open() of the class.
(a) Constructor method
This method is applicable only when we are managing only one file with filestream
Managing Single File
As we all know about the constructor that is used for initializing the object.
a) Select a class and create an object
b) Create object depending upon the purpose we want to open that file. Object creation depends upon the read and writes operations of the file i.e. ifstream or ofstream.
c) The object should be initialized with the file The filename should be given to initialize that object.
d) Objects are used to open a file.
If we want to create a file named as codingtag and wants to write some information in that file. The output stream can be created with the use of ofstream.
Ofstream outfile("codingtag"); //output
Where Ofstream is the class and outfile is its object. This statement is used to open the file, write and to link it with the output stream. Let's study diagrammatically how objects are used to open file.
Ifstream infile("codingtag1"); //input
This statement not only create codingtag file but also perform read operation on it.
CLOSING A FILE
Object. close() can be used to disconnect any stream.
Where outfile is an object of the ofstream class
(b) Member function open()
Managing Multiple Files
Suppose we have a class named as Ofstream with its object as outfile. With single stream we can read 4 files i.e. F1, f2, f3, f4. A single object can attach only one file at a time, therefore, you need to disconnect the first file with close function before using the second file.
Simple program of creating a file with the help of constructor method
using namespace std;
cout<<"enter article name\n";
cout<<"enter article cost\n";
enter article name
enter article cost