您的位置:首页 > 编程语言 > C语言/C++

定义文件流对象,统计字符个数(C/C++源程序)

2015-09-29 16:52 253 查看
#include<iostream>

#include<fstream>

using namespace std;

int main()

{

 char *str=" I study C++ in ok2002.com";

 int count_k=0,count_all=0;

 ofstream rs;

 rs.open("ok2002.txt",ios::out);

 if(!rs)

  cout<<"文件已打开"<<endl;

 for(int i=0;str[i]!=0;i++)

 {

  if(str[i]=='k')

   count_k++;

   rs.put(str[i]);

count_all++;

 }

 rs.close();

 cout<<"字符总数是:"<<count_all<<endl;

 cout<<"其中字符k的总数是:"<<count_k<<endl;

}

转自: http://www.ok2002.com/

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: