Professional Documents
Culture Documents
Files
Files
C++ FILES
So far, we have been using the iostream standard library, which provides cin and
cout methods for reading from standard input and writing to standard output res
pectively.
This tutorial will teach you how to read and write from a file. This requires an
other standard C+ + library called fstream, which defines three new data types:
out.txt
Anand
Note: We have to create only in.txt. After executing this program, out.txt will
be created.
//Vowel count
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
char c;
int ac,ec,ic,oc,uc;
void main()
{ clrscr();
ifstream fin("in.txt");
ofstream fout("out.txt");
while(1)
{ fin>>c;
if(fin.eof()) break;
if((c=='a')||(c=='A')) ac++;
else if((c=='e')||(c=='E')) ec++;
else if((c=='i')||(c=='I')) ic++;
else if((c=='o')||(c=='O')) oc++;
else if((c=='u')||(c=='U')) uc++;
}
fout<<"Number of a "<<ac<<endl;
fout<<"Number of e "<<ec<<endl;
fout<<"Number of i "<<ic<<endl;
fout<<"Number of o "<<oc<<endl;
fout<<"Number of u "<<uc<<endl;
cout<<"Done-open out.txt and check";
getch();
}