第十四周项目1(3)-小玩文件

来源:互联网 发布:沈阳哪里有mac专柜 编辑:程序博客网 时间:2024/05/18 00:16
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:week14-1-3.cpp *作者:高赞 *完成日期: 2015 年 6 月 6 日 *版本号:v1.0 * *问题描述:统计字母和数字的个数 * */#include <iostream>#include <cstdlib>#include <fstream>using namespace std;int main(){    int j=0,k=0;    string name;    cin>>name;    fstream file;    file.open(name.c_str(), ios::in);//c_str()吧string 改为 char*    if(!file)    {        cout<<name<<" can’t open."<<endl;        exit(1);    }    char ch;    while( !file.eof())    {        file.get(ch);        if(ch>='0'&&ch<='9')            ++j;        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))            ++k;    }    cout<<"字母个数:"<<k<<endl        <<"数字个数:"<<j<<endl;    file.close();    return 0;}



输入文件名时要加上后缀类型;

打开文件时文件名路径似乎不能是string变量,用c_str转换成char*型可以通过编译。

0 0