File Handling
File Handling
Introduction
Program
Output stream
insert
Output
Into
device
output
stream
Fig: Data Stream
iostream
Fig: shows hierarchy of the stream classes used for i/p & o/p operations
with the console unit.
Stream classes for Console Operations
Class name Contents
ios Basic facilities that are used by all other
(general input/output stream input-output classes.
class) Also contain a pointer to buffer object
(streambuff object)
Istream Inherits properties of ios
(input stream) Has functions like get( ), getline( ),
read( )
Contains overloaded operator >>
Ostream Inherits properties of ios
(output stream) Put( ), write( )
Contains overloaded operator <<
Iostream Inherits properties of istream and
(input/ output stream) ostream through multiple inheritance
and thus contains all input and output
functions
Stream Errors
Disk
Output stream
Result
file
outfile
Program
Input stream
Data file
infile
Program1
---------
---------
Ofstream outfile( “salary”) ; //create outfile & connects “salary” to it.
--------
---------
Program2
----------
---------
Ifstream infile (“salary”) ; //create infile & connects “salary” to it
---------
---------
Program1 outfile
----------
----------
Salary
file
Program2
infile
----------
----------
Ms
Use of single program to do both the operation on a file.
The program would contain following statements:
---------
---------
ofstream outfile(“salary”) ; //create outfile & connects “salary” to it.
--------
---------
Outfile.close( ); //disconnect salary from outfile
ifstream infile (“salary”) ; //and connect to infile
---------
---------
Infile.close( ); //disconnect salary form infile
outfile
Salary
Program file
infile
inf.close();
return 0;
}
Opening a File Using open()
• The function open( ) can be used to open multiple files that use
the same stream object.
• In such a cases, first create a single stream object & use it to
open each file in turn.
• This is done as follows:
file-stream-class stream-object ;
Stream-object . open( “filename” , mode ) ;
Here , mode specifies the purpose for which the file is opened.
File mode parameter Meaning
ios::app Append to end of file
ios::ate go to end of file on opening
ios::binary file open in binary mode
ios::in open file for reading only
ios::out open file for writing only
ios::nocreate open fails if the file does not exist
ios::noreplace open fails if the file already exist
ios::trunc delete the contents of the file if it exist
Open and close a file
eg:-
ofstream outfile; // create stream
outfile.open (“DATA1”); // connect stream to DATA1
……………………………..
……………………………..
outfile.Close(); //disconnect stream from DATA1
outfile.open(“DATA2”); //connect stream to DATA2
……………………………..
……………………………..
outfile . close( );
……………………………..
Opening a File Using open()
#include<iostream.h>
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("Country");
fout<<"US\n";
fout<<"UK\n";
fout.close();
fout.open("Capital");
fout<<"Washington\n";
fout<<"London\n";
fout.close();
fin.close();
fin.open("Capital");
cout<<"\nContents of Capital file \n";
while(fin)
{
fin.getline(line,N);
cout<<line<<"\n";
}
fin.close();
return 0;
}
Detecting end-of-file
• Detection of the end of file is necessary for preventing any further
attempt to read the data from the file.
The statement while(fin) detect end of file.
• An ifstream object such as fin, returns a value 0 if any error occurs in
the file operation including the enf-of-file condition.
• Thus, the while loop terminates when fin returns a value 0 on
reaching the end-of-file condition.
Following fig shows the action on file pointer while opening a file
Functions for Manipulating File Pointers
When we want to move file pointer to desired position then use these
function to manage the file pointers.
seekg ( ) = moves get pointer (input) to a specified location
seekp ( ) = moves put pointer (output) to a specified location
Function Operation
open( ) To create a file
close( ) To close an existing file
get( ) Read a single character from a file
put( ) write a single character in file.
read( ) Read data from file
write( ) Write data into file.
Put( ) and get( ) functions
• The function put() write a single character to the
associated stream. Similarly, the function get() reads
a single character from the associated stream.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
{
char str[30];
clrscr();
cout<<"\nEnter String:";
cin.getline(str,30);
fstream fs;
fs.open("Sample.txt",ios::in|ios::out);
for(int i=0;str[i]!='\0';i++)
fs.put(str[i]);
}
fs.close();
cout<<"\nDisplay contents of file";
fs.open("Stud1.data",ios::in|ios::binary);
Stud s1;
fs.read((char*)&s1,sizeof(s1));
while(fs)
{
s1.putdata();
fs.read((char*)&s1,sizeof(s1));
}
fs.close();
getch();
return 0;
}
Overloading stream insertion (<<) and extraction (>>) operators
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{
real = r; imag = i;
}
friend ostream & operator << (ostream &out, Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << “\n Enter Imagenory Part ";
in >> c.imag;
return in;
}
Output:
• Enter Real Part 10
• Enter Imaginary Part 20
• The complex object is 10+i20
Command line arguments in C++
#include<iostream>
using namespace std;
int main(int argc,char** argv)
{
cout<<"\nARGC:"<<argc;
for(int i=0;i<argc;i++)
cout<<argv[i]<<"\n";
return 0;
}
Output
Memory as a Stream Object