FILE HANDLING
FILE HANDLING
The file loading technique of C++ support file manipulation in the form of stream object. The stream objects are predefined in the header file, iostream.h. There are no predefined objects for disk files. All class declaration have to be done explicitly in the program.
Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow. These are three classes for handling files.
ifstream - It is derived from istream, is used for file input.
ofstream - It is derived from ostream, is used for file output.
fstream - It is derived from iostream, is used for both file input and output.
FILE STREAM CLASS HIERARCHY:
File Input and Output:
To perform file I/O, the header file fstream.h must be included in the program. It defines several classes, including ifstream, ostream and fstream. These classes are derived from ios. So ifstream, ofstream and fstream also have full access to all operations defined by ios.
Open mode Bits:
In an ios class, the bits that are associated with the opening of a file are called open mode bits. The open mode bits state the method in which a file is to be opened. The different file opening modes are:
in : Opens a file for reading only.
out : Opens a file for writing only.
app : Appends at the end of an existing file without deleting the previous details.
ate : Seeks to the end-of-file.
trunc : Trunctates the file, if it exists.
Reading / Writing Data from / to a Disk File:
get() : It is a class member function to retrieves a string, one character at a time.
put() : It is a class member function to writes the string, one character at a time.
Note: File streams have open() and close() member functions to open and close a file respectively.
// Ex-35: ASSIGNING VALUES INTO THE FILE AND READ THE SAME
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream out("SYSTEM");
out<<"KEYBOARD "<<1205.50<<endl;
out<<"MONITOR "<<3525.75<<endl;
out<<"CPU "<<7350<<endl;
out.close();
ifstream in("SYSTEM");
char item[20];
float price;
in>>item>>price;
cout<<item<<" "<<price<<"\n";
in>>item>>price;
cout<<item<<" "<<price<<"\n";
in>>item>>price;
cout<<item<<" "<<price<<"\n";
in.close();
getch();
}
// Ex-36: PGM ILLUSTRATES TO WRITE A TEXT FILE
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main(void)
{
clrscr();
ofstream out;
out.open("demo.dat");
if(out.fail())
{
cout<<"Error: File opening"<<endl;
exit(0);
}
int i;
while(1)
{
i=cin.get();
if(i==-1) // -1 is equalent to ctrl+Z
break;
else
out.put((char)i);
}
out.close();
}
// Ex-37: PGM ILLUSTRATES TO READ THE CONTENT OF EXISTING TEXT FILE
#include<conio.h>
#include<iostream.h>
#include<process.h>
void main()
{
clrscr();
ifstream in;
in.open("demo.dat");
if(in.fail())
{
cout<<"Error : File opening"<<endl;
exit(0);
/* To execute this stmt we should include the header file
'process.h / stdlib.h' */
}
int i;
while(1)
{
i=in.get();
if(i==-1)
break;
else
cout<<(char)i;
}
in.close();
getch();
}