15第十四周项目一——小玩文件

来源:互联网 发布:淘宝客服简历模板 编辑:程序博客网 时间:2024/03/29 14:45

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:李晓凯
 * 完成日期:2015年 6 月 7 日
 * 版 本 号:v1.0
 *
 * 问题描述:完成程序,使其能统计abc.txt文件的字符个数
 * 输入描述:无
 * 程序输出:字符的个数

 */

(1)

#include <iostream>#include <cstdlib>#include _____________ // (1)using namespace std;int main(){    fstream file;    file.open("abc.txt", _________); // (2)    if(!file) {        cout<<"abc.txt can’t open."<<endl;        exit(1);    }    char ch;    int i=0;    while( _____________) // (3)    {        file.get(ch);         _____________; // (4)    }    cout<<"Character: "<<i<<endl;    file._____________;// (5)    return 0;}
答案:

(1)fstream

(2)ios::in

(3)!file.eof()

(4)close()


         


(2)

#include <iostream>#include <cstdlib>#include <fstream>using namespace std;int main(){    fstream outfile,infile;    infile.open("abc.txt",_________); // (1)    if(!infile) {        cout<<"Can’t open the file."<<endl;        abort();    }    outfile.open("newabc.txt",______);//(2)    if(!outfile) {        cout<<"Can’t open the file."<<endl;        abort();    }    char buf[80];    int i=1;    while(____________) // (3)    {        infile.____________; // (4)        outfile<<________<<": "<<buf<<endl; //(5)    }    infile.close();    outfile.close();    return 0;}

(1)ios::in

(2)ios::out

(3)!infile.eof()

(4)getline(buf,80)

(5)i++



(3)用键盘输入文件名, 统计输出文件中每个字母,数字的个数

#include <iostream>#include <cstdlib>#include <fstream>using namespace std;int main(){    fstream outfile,infile;    infile.open("aboutapp.txt",ios::in); // (1)    if(!infile)    {        cout<<"Can’t open the file."<<endl;        abort();    }    char ch;    int n=0,m=0;    while(!infile.eof()) // (3)    {        infile.get(ch);        if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')            n++;        if(ch>='0'&&ch<='9')            m++;    }    cout<<"字母:"<<n<<endl<<"数字:"<<m<<endl;    infile.close();    outfile.close();    return 0;}


0 0