- #include<iostream>
- #include<fstream>
- #include<cstdlib>
- using namespace std;
- class CntCharacters
- {
- private:
- int cnt;
- public:
- CntCharacters():cnt(0){}
- ~CntCharacters(){}
- void opentxt(char* p)
- {
- ifstream fin;
- fin.open(p,ios_base::in);
- if(!fin.is_open())
- {
- cout<<"cannot open the file,Please make sure the file is exist!\\n";
- exit(-1);
- }
- char temp;
- while(!fin.eof())
- {
- fin>>temp;
- if((temp>='a'&&temp<='z')||(temp>='A'&&temp<='Z'))cnt++;
- }
- }
- void countthecharacter()
- {
- int count=0;
- char nameoffile[80];
- cout<<"Please enter the name of file:";
- cin>>nameoffile;
- // scanf("%s",nameoffile);
- opentxt(nameoffile);
- }
- void dis()
- {
- cout<<cnt<<endl;
- }
- };
- int main()
- {
- CntCharacters* c=new CntCharacters;
- c->countthecharacter();
- c->dis();
- delete c;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0810201513749.html
来源: http://www.codesnippet.cn/detail/0810201513749.html