7FILE HANDLING Ver7
7FILE HANDLING Ver7
read ( )
The member function of class istream requires
exactly 2 parameters.
1. The pointer to character (array of character)
2. integer (i.e. the number of character to read
from the stream)
#include<conio.h>
#include<iostream.h>
void main( )
Page 3 of 77 Satpal Singh(9811969092) File Handling V7
{
char a,b,c,d;
cout<<"\nEnter data1 ";
cin>>a>>b;
cout<<"a is "<<a<<" b is "<<b;
cout<<"\nEnter data2 ";
cin>>c>>d;
cout<<"c is "<<c<<" d is "<<d;
getch( );
}
Output:
Example 1 (Normal functioning):
Enter data1 k
l
a is k b is l
Enter data2 y
u
c is y d is u
Example 2:
Enter data1 rtyu
a is r b is t
Enter data2 c is y d is u
Example 4:
Enter data1 pou
a is p b is o
Enter data2 g
c is u d is g
Example 5:
Enter data1 kumari
a is k b is u
Enter data2 c is m d is a
// ri and enter is still in the stream.
Enter data2
Enter data3
Enter data4
Enter data5
Enter data6 kjh
a is d b is u
c is s d is t
e is
f is k
Enter data1 q 1
Enter data2
Enter data3
Enter data4
Enter data5 udcvbn
Enter data6
a is q b is
c is 1 d is
e is u f is d
Enter data2
Enter string1
Enter data4
Enter data5
Enter string2
a is c b is o
g is mputer d is
e is s h is tudent
// space and tanvi and enter is still in stream
Enter data2
Enter string1
Enter data4
Enter data5
Enter string2
a is h b is a
g is rd d is
Page 10 of 77 Satpal Singh(9811969092) File Handling V7
e is w h is ork
// space is the key of success enter is still in stream
Enter data2
Enter string1
Enter data4 fghijklmnopqrst
Enter data5
Enter string2
a is a b is b
g is cde d is f //and also the enter shell be
removed from the stream
e is g h is hijklmnop //9 characters
Enter string 2
Enter string 3
Enter string 4
String 1 is it is a t
String 2 is est of co
String 3 is nor of co
String 4 is puters
Enter string 2 gh
ijk
lmrstu
Enter string 4
String 1 is abc
String 2 is def
gh
ijk
lm
String 3 is stu
co
String 4 is puter
gets( )
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
void main( )
{
char a,b,c,d,e,f,g[10],h[10];
cout<<"\nEnter data1 ";
cin.get(a);
cout<<"\nEnter data2 ";
cin.get(b);
cout<<"\nEnter string1 ";
gets(g);
cout<<"\nEnter data4 ";
cin.get(d);
cout<<"\nEnter data5 ";
cin.get(e);
cout<<"\nEnter string2 ";
gets(h);
cout<<"\na is "<<a<<" b is "<<b;
cout<<"\ng is "<<g<<" d is "<<d;
Page 18 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\ne is "<<e<<" h is "<<h;
getch( );
}
Enter data2
Enter string1 tanvi//the data is there in stream but
gets( ) would read the data from the k/b
Enter data4
Enter data5
Enter string2 pancholi
a is a b is b
g is tanvi d is c
e is d h is pancholi //pancholi is given through k/b
modes
ios::in //for reading purpose
ios::out //creates a new file (or deletes the existing
text from the file)
if not used with ios::in or ios::app
ios::app always write at the end of the file
//modification of existing data is impossible
Page 22 of 77 Satpal Singh(9811969092) File Handling V7
ios::binary for binary files
fstream d("out.txt",ios::out);
fstream e;
e.open("out.txt"); //error the member function
open of class fstream requires 2 args.
fstream e;
e.open("out.txt",ios::out);
*/
//if the file fails to open the value of the file
object automatically sets as NULL
if(!a) //if(a==NULL)
{
Page 24 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\nFile opening error ";
return;
}
a.put('t');
a.put('o');
a.put('y');
a<<’ ‘<<’i’<<’s’<<’ ‘;
a<<”soft”;
a.write(“and silky”,9);
a.close( );
}
#include<fstream.h>
#include<conio.h>
void main( )
{
ofstream a("out.txt"); //invoking 1 arg constructor
if(!a) //if (a==NULL)
{
cout<<"\nFile opening error ";
return;
}
a.put('t');
a.put('o');
a.put('y');
a.close( );
}
#include<fstream.h>
#include<conio.h>
void main( )
{
ofstream a("out.txt"); //invoking 1 arg constructor
if(!a) //if (a==NULL)
{
cout<<"\nFile opening error ";
return;
}
char ch;
//reading : once read before the loop and one at
the end of the loop
cin.get(ch);
while(ch!='#') //while(ch!=EOF) //EOF control +
z shell be the delimiter
{
a.put(ch);
Page 27 of 77 Satpal Singh(9811969092) File Handling V7
cin.get(ch);
}
a.close( );
}
//If the input is “This is a good#school” The file
contents shell be “This is a good”
#include<fstream.h>
#include<conio.h>
void main( )
{
ofstream a("out.txt"); //invoking 1 arg constructor
if(!a) //if (a==NULL)
{
cout<<"\nFile opening error ";
return;
}
char ch;
Page 28 of 77 Satpal Singh(9811969092) File Handling V7
while(ch!='#')
{
cin.get(ch);
a.put(ch);
}
a.close( );
}
D e a r S t u d e n t s EO
ios::beg 0 1 2 3 4 5 6 7 8 9 10 11 12 13
- - - - - - - - - - - -
ios::end 13 12 11 10 9 8 7 6 5 4 3 2 -1 0
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void main( )
{
clrscr( );
ifstream a("out.txt"); //invoking 1 arg constructor
if(!a) //if (a==NULL)
{
Page 30 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\nFile opening error ";
return;
}
char ch;
cout<<isdigit('t');
//Output shell be 0 (false) if the input is not in
between '0' and '9' (48-57) otherwise non zero
a.get(ch);
while(a.eof( )==0) //while(ch!='t')
{
//cout.put(ch);
cout<<ch;
a.get(ch);
}
a.close( );
getch( );
}
#include<fstream.h>
#include<conio.h>
void main( )
{
clrscr( );
ifstream a("out.txt"); //invoking 1 arg constructor
if(!a) //if (a==NULL)
Page 33 of 77 Satpal Singh(9811969092) File Handling V7
{
cout<<"\nFile opening error ";
return;
}
char ch;
a.get(ch);
//while(a.eof( )==0) // best to
understand
//while(!a.eof( )) //it is most trendy
//as soon as the object fails to read, the value of
its object
//automatically sets as NULL
while(a) //Method 3 //it should be
avoided
{
//cout.put(ch);
cout<<ch;
a.get(ch);
}
a.close( );
getch( );
}
#include<fstream.h>
#include<conio.h>
void main( )
{
clrscr( );
ifstream a("out.txt"); //invoking 1 argument
constructor
if(!a) //if (a==NULL)
{
cout<<"\nFile opening error ";
return;
}
char ch;
Page 36 of 77 Satpal Singh(9811969092) File Handling V7
while(a.get(ch))
cout<<ch;
a.close( );
getch( );
}
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void count(char fn[]) //fn is formal
parameter
{
Page 39 of 77 Satpal Singh(9811969092) File Handling V7
ifstream fin(fn);
if(!fin)
{
cout<<"\nFile opening error ";
return;
}
int u=0,d=0,s=0,n=0;
char ch;
while(fin.get(ch))
{
if(isupper(ch)) //if(ch>='A' && ch<='Z')
u++;
if(isdigit(ch)) //if(ch>='0' && ch<='9')
d++;
if(ch==' ')
s++;
if(ch=='\n')
n++;
}
cout<<u<<"\t"<<d<<"\t"<<s<<"\t"<<n;
fin.close( );
}
void main( )
{
Page 40 of 77 Satpal Singh(9811969092) File Handling V7
clrscr( );
count("mouse.txt"); //mouse.txt is the
actual parameter
getch( );
}
w.seekg(2); //w.seekg(2,ios::beg);
cout<<"\nThe current position is : "<<w.tellg( );
w.put('Q');
cout<<"\nThe current position is : "<<w.tellg( );
w.seekp(1);
w.put('A'); //w.seekg(2,ios::beg);
cout<<"\nThe current position is : "<<w.tellg( );
w.seekp(3,ios::cur);
w.put('F');
cout<<"\nThe current position is : "<<w.tellg( );
w.seekp(-3,ios::end);
w.put('R');
cout<<"\nThe current position is : "<<w.tellg( );
D e a R S t u d e n t s EO
ios::beg 0 1 2 3 4 5 6 7 8 9 10 11 12 13
- - - - - - - - - - - -
ios::end 13 12 11 10 9 8 7 6 5 4 3 2 -1 0
Let the contents of the text file cpu.txt is "Dear Students"
D A Q r F t u d e R t s EO
ios::beg 0 1 2 3 4 5 6 7 8 9 10 11 12 13
- - - - - - - - - - - -
ios::end 13 12 11 10 9 8 7 6 5 4 3 2 -1 0
Output:
The current position is : 0
The current position is : 2
The current position is : 3
The current position is : 2
The current position is : 6
The current position is : 11
Contents of cpu.txt
DAQr FtudeRts
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void replace(char fn[],char x, char y)
{
char ch;
//clrscr( );
fstream w(fn,ios::in | ios::out); //invoking 1 arg
constructor
while(w.get(ch))
if(ch==x)
{
w.seekg(-1,ios::cur);
w.put(y);
}
Page 46 of 77 Satpal Singh(9811969092) File Handling V7
w.close( );
}
void main( )
{
char a[30],b,c;
cout<<"\nEnter file name and 2 characters ";
cin>>a>>b>>c;
//replace("cpu.txt",'t','Z');
replace(a,b,c);
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void toggle(char fn[])
{
char ch;
fstream w(fn,ios::in | ios::out); //invoking 1 arg
constructor
while(w.get(ch))
{
if(isupper(ch))
{
Page 48 of 77 Satpal Singh(9811969092) File Handling V7
w.seekg(-1,ios::cur);
w.put(tolower(ch));
}
else
if(islower(ch))
{
w.seekg(-1,ios::cur);
w.put(toupper(ch));
}
else
if(isdigit(ch))
{
w.seekg(-1,ios::cur);
if(ch=='9')
w.put('0');
else
w.put(ch+1);
}
}
w.close( );
}
void main( )
{
char a[30];
Page 49 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\nEnter file name ";
cin>>a;
toggle(a);
}
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void main( )
{
Page 51 of 77 Satpal Singh(9811969092) File Handling V7
char a[30];
int count=0;
clrscr( );
fstream r("cpu.txt",ios::in);
while(r>>a)
{
cout<<a;
cout<<" "<<++count<<endl;
getch( );
}
getch( );
}
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void main( )
{
char a[30];
int count=0;
clrscr( );
fstream r("cpu.txt",ios::in);
r>>a;
while(r.eof( )==0) //while(!r.eof( ))
{
Page 53 of 77 Satpal Singh(9811969092) File Handling V7
count++;
cout<<a;
r>>a;
}
cout<<”\nThe number of words is “<<count;
getch( );
}
Example
Contents of the source file:
The students of the class twelfth
are teenagers and they
Page 55 of 77 Satpal Singh(9811969092) File Handling V7
should trust themself as they are the best.
Output
9
#include<fstream.h>
#include<conio.h>
int copy(char f1[],char f2[])
{
char a[30];
clrscr( );
fstream r(f1,ios::in);
fstream w(f2,ios::out);
int count=0;
while(r>>a)
if(a[0]=='t' || a[0]=='T')
{
count++;
w<<a<<" ";
}
return count;
}
Page 56 of 77 Satpal Singh(9811969092) File Handling V7
void main( )
{
cout<<copy("a.a","b.b");
getch( );
}
#include<fstream.h>
#include<conio.h>
int copy(char f1[],char f2[])
{
char string[80];
clrscr( );
fstream r(f1,ios::in);
fstream w(f2,ios::out);
int count=0;
while(r.getline(string,80))
{
count++;
w<<string<<'\n';
}
Page 58 of 77 Satpal Singh(9811969092) File Handling V7
return count;
}
void main( )
{
cout<<copy("a.a","b.b");
}
#include<conio.h>
#include<fstream.h>
#include<process.h>
struct emp
{
int eno;
char name[98];
};
void ABC(int N)
{
Page 60 of 77 Satpal Singh(9811969092) File Handling V7
ofstream fout("emp.dat",ios::binary);
// fstream fout("emp.dat",ios::binary | ios::out);
if(!fout)
{
cout<<"\nFile opening error";
exit(0); //process.h
}
emp x;
for(int i=0;i<N;i++)
{
cout<<"\nEnter emp and name : ";
cin>>x.eno>>x.name; //x.get_data( ) if
emp is class
fout.write((char*)&x,sizeof(emp));
//fout.write((char*)&x,sizeof(x));
}
fout.close( );
}
void main( )
{
clrscr( );
int N;
cout<<"\nEnter the number of records : ";
Page 61 of 77 Satpal Singh(9811969092) File Handling V7
cin>>N;
ABC(N);
#include<conio.h>
#include<fstream.h>
#include<process.h>
struct emp
{
int eno;
Page 62 of 77 Satpal Singh(9811969092) File Handling V7
char name[98];
};
void Display( )
{
ifstream f("emp.dat",ios::binary);
if(!f)
{
cout<<"\nFile opening error";
exit(0);
}
emp x;
while(f.read((char*)&x,sizeof(emp))) //three
close parenthesis sizeof, read, while
cout<<"\nThe emp and name : "<<x.eno<<"
"<<x.name;
//x.show_data( ) if emp is a class
f.close( );
}
void main( )
{
clrscr( );
Display( );
getch( );
Page 63 of 77 Satpal Singh(9811969092) File Handling V7
}
#include<conio.h>
#include<fstream.h>
#include<process.h>
struct emp
{
int eno;
char name[98];
};
void Modify(int N)
{
fstream f("emp.dat",ios::binary | ios::in | ios::out);
if(!f)
{
Page 65 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\nFile opening error";
exit(0);
}
//count the total number of records existing in the
binary file
int s=sizeof(emp);
f.seekp(0,ios::end);
int pos=f.tellp( );
int records=pos/s;
if(N>records)
{
cout<<"\nThe total number of records in the
file is "<<records;
cout<<"\nPlease enter the number
<="<<records;
exit(0);
}
emp x;
cout<<"\nEnter emp and name for new record : ";
cin>>x.eno>>x.name; //x.get_data( ) if x is an
object of class emp
f.seekp(s*(N-1));
f.write((char*)&x,sizeof(emp));
cout<<"\nRecord Modified ";
Page 66 of 77 Satpal Singh(9811969092) File Handling V7
f.close( );
}
char ch='T';
w.put('K');
w.put(ch);
w.put(ch);
Page 72 of 77 Satpal Singh(9811969092) File Handling V7
w.close( );
}
cout<<isspace('\r');//ASCII 13
cout<<isspace(' ');//ASCII 32
cout<<"\nout 1 "<<isspace(9);
cout<<"\nout 2 "<<isspace(10);
Page 74 of 77 Satpal Singh(9811969092) File Handling V7
cout<<"\nout 3 "<<isspace(11);
cout<<"\nout 4 "<<isspace(12);
cout<<"\nout 5 "<<isspace(13);
cout<<"\nout 6 "<<isspace(32);
cout<<"\nout 7 "<<isspace('A');//0
getch( );
}
Output:
Enter an integer :
3445 mouse
Enter a character :
a is 3445 b is m
1111
out 1 1
out 2 1
out 3 1
out 4 1
out 5 1
out 6 1
out 7 0
Note: ouse + enter is still in the stream.
Page 75 of 77 Satpal Singh(9811969092) File Handling V7
put( )
Note: The put( ) function would write the output to
the output stream and the cout would flush the
stream.or the stream shell be flush automatically
when the program would terminate.
#include<conio.h>
#include<iostream.h>
void main( )
{
int a=99;
clrscr( );
cout.put(a++);
cout.put(a++);
cout.put(a++);
getch( ); //nothing shell be displayed till
this point
cout<<"";
getch( ); //the output cde shell be visible
cout.put(a++);
cout.put(a++);
cout.put(a++);
cout.put(a++);
Page 76 of 77 Satpal Singh(9811969092) File Handling V7
getch( ); //still the output only cde is visible
//the rest of the output i.e. fghi is still
in output stream
//it shell be flushed automatically as
soon as the program would
//terminate
}
Code segment:
cout<<endl<<ios::in; //1
cout<<endl<<ios::out;//2
cout<<endl<<ios::app; //8
cout<<endl<<ios::binary;//128
//The multiple modes can be used together using |
(bit-wise OR) (Pipe symbol)