File Handling
2 4257
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
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
Write
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.
FILE
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
Alphabetic order
Descending /Ascending order
Chronological order
Text files
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
# Objective
# Opening Method
A suitable name for the File
When we want to create any file, a suitable name is required.
For e.g.
codingtag.doc which consist of the file name and its extension
File Created | File Name | Extension |
codingtag.txt | codingtag | .txt |
codingtag.doc | codingtag | .doc |
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.
Steps
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.
For example
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.
For example
Outfile.close();
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.
Syntax
Stream-object.open("file name");
Example:
Ofstream outfile;
outfile.open("File1");
.... //code
outfile.close();
outfile.open("File2");
.... //code
outfile.close();
outfile.open("File3");
.... //code
outfile.close();
outfile.open("File4");
.... //code
outfile.close();
Simple program of creating a file with the help of constructor method
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char name[20];
float cost;
ofstream outf("CODINGTAG");
cout<<"enter article name\n";
cin>>name;
cout<<"enter article cost\n";
cin>>cost;
return 0;
}
Output:
enter article name
enter article cost
Share:
Coding Tag Aug 19, 2019
Hye Akash, Now check.. Thanks for your feedback.
Akash Aug 05, 2019
What r those characters written before code