我们先来看看下面的代码:
#include
#include
#include
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<
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<
cin>>nameoffile;
// scanf("%s",nameoffile);
opentxt(nameoffile);
}
void dis()
{
cout<
}
};
<
本文展示了三种C++代码示例,分别用于统计文件中包含空格和换行在内的所有字符数,以及排除空格和换行后的字符数。还提供了一个项目中实际使用的代码片段,该片段读取命令行参数作为文件名并计算其字符总数。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



