This is a program that would help you to learn about the basics of a file through c++. This would teach ue that how can we acess a file, of how can we open it, how can we cloase it, how can we acess the documents present in a file. Its easy to use just install a c++ compiler copy this program to a new notepad and save it as a .cpp file and copy it in the bin of c++ compiler. Its all done now just open compiler search for the file open it and run it.
#include<stdlib.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{ clrscr();
fstream f1("mydata.dat",ios::in);
char ch;
int cnt=0;
char c;
if(!f1)
{ cout<<"\n cannot open";
getch();
exit(-1);
}
cout<<"\n the text document is :";
while (!f1.eof())
{ f1.get(c);
cout<<c;
}
f1.close();
f1.open("mydata.dat",ios::in);
while (!f1.eof())
{ f1.get(ch);
if(isalpha(ch))
cnt++;
}
cout<<"\n number of alphabets are:"<<cnt;
f1.close();
getch();
}
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{ clrscr();
fstream f1("mydata.dat",ios::in);
char ch;
int cnt=0;
char c;
if(!f1)
{ cout<<"\n cannot open";
getch();
exit(-1);
}
cout<<"\n the text document is :";
while (!f1.eof())
{ f1.get(c);
cout<<c;
}
f1.close();
f1.open("mydata.dat",ios::in);
while (!f1.eof())
{ f1.get(ch);
if(isalpha(ch))
cnt++;
}
cout<<"\n number of alphabets are:"<<cnt;
f1.close();
getch();
}
EmoticonEmoticon