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

来源:互联网 发布:unity3d找工作好难 编辑:程序博客网 时间:2024/05/29 18:16

#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/

 

0 0