(字符计数)编写一个程序,提示用户输入文件名,显示文件中字符的数量。
不废话,水题上代码:
以下代码在Dev-C++上获得通过!
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string fileName;
cout<<"Enter a file:";
cin>>fileName;
ifstream input(fileName.c_str());// .c_str()把string类转化成C字符串
if(input.fail())
{
cout<<"File does not exist!"<<endl;
cout<<"Exit program"<<endl;
return 0;
}
else
{
cout<<fileName<<" exists"<<endl;
}
cout<<"The number of string is ";
char a;
int count=0;
while(input>>a)
{
count++;
}
cout<<count<<endl;
return 0;
}