实验6 流类库与I/O

Part1 验证性实验

1. 合并两个文件到新文件中。文件名均从键盘输入。

// 合并两个文件内容到一个新文件中。
// 文件名均从键盘输入

#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string filename1, filename2, newfilename;
    
    cout << "输入要合并的两个文件名: " ;
    cin >> filename1 >> filename2;
    cout << "输入合并后新文件名: " ;
    cin >> newfilename;
    
    ofstream fout;        // 输出文件流对象 
    ifstream fin;        // 输入文件流对象 
    
    
    fin.open(filename1);  // 将输入文件流对象fin与文件filename1建立关联 
    if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
        cerr << "fail to open file " << filename1 << endl;
        system("pause");
        exit(0);    
    }
    
    fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
    if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        exit(0);
    }
    
    char ch;
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
    while(fin.get(ch)) 
        fout << ch;
    
    fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
    
    fout << endl; // 向文件输出流对象fout中插入换行 
    
    
    fin.open(filename2);  // 将输入文件流对象fin与文件filename2建立关联 
    if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
        cerr << "fail to open file " << filename2 << endl;
        system("pause");
        exit(0);    
    }
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
    while(fin.get(ch))
        fout << ch;
    
    fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
} 
ex1

Part2 基础练习
使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "

#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string filename;
    cout << "打开文件: " ;
    cin >> filename;
    ofstream fout(filename,ios_base::app);        
    if(!fout.is_open()) { 
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);    
    }
    fout<<endl<<"merge successfully."<<endl;
    fout.close(); 
    return 0;
} 
ex2

Part3 应用编程实践
1. 已知名单列表文件list.txt。编写一个应用程序,实现从名单中随机抽点n位同学(n由键盘输入),在屏幕上显 示结果,同时也将结果写入文本文件,文件名自动读取当天系统日期,如20190611.txt。

#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include<sstream>
#include<ctime>
#include "utils.h"

using namespace std;

int main() {
    ifstream fin;
    ofstream fout;
    string filename;
    int n, totalline = 0;
    cout << "输入名单列表文件名:";
    cin >> filename;
    cout << "输入随机抽点人数:";
    cin >> n;
    fin.open(filename, ios_base::in);
    if (!fin.is_open()) {
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);
    }
    string s, a[100];
    if (fin) {
        while (getline(fin, s)) {
            a[totalline++] = s;
        }
        fin.close();
    }
    string filename1;
    filename = getCurrentDate() + ".txt";
    cout << "随机抽点中..." << filename1<< endl;
    srand(std::time(0));
    for (int i=1;i<=n;i++) {
        int num;
        int rand0 = rand();
        num= rand() % totalline;
        cout <<  a[num] << endl;
        fout.open(filename, ios_base::app);
        fout << a[num] << endl;
        fout.close();
    }
    system("pause");
    return 0;
}
main
#include <string>
using std::string;
string getCurrentDate();
utils.h
#include "utils.h" 
#include <ctime> 
using std::string;
const int SIZE = 20;
// 函数功能描述:返回当前系统时间 
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611 
string getCurrentDate() {        
    time_t time_seconds = time(0);    
    struct tm now_time;    
    localtime_s(&now_time, &time_seconds); // 使用了更安全的localtime_s()       
    char date[SIZE];
    strftime(date, SIZE, "%Y%m%d", &now_time);        
    return (string(date));
}
utils.cpp

2. 编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。 

#include <iostream>
#include <fstream>   
#include <string>
using namespace std;
int main() {
    string filename;      
    cout<<"输入要统计的文本文件名:" ;
    cin>>filename;
    ifstream fin;
    ofstream out;
    fin.open(filename);
    if(!fin.is_open()) {
        cerr << "fail to open file "<<endl;
        system("pause");
        exit(0);    
    }
    char ch;
    int c=0,w=0,l=1;
    while(fin.get(ch)){
        if(ch==' '){
            w++;
            c++;
        }
        else if(ch!=' '&&ch!='\n'){
            c++;
        }
        else{
            l++;
        }
    }
        cout<<"字符数:"<<c<<endl;
        cout<<"单词数:"<<w+l<<endl;
        cout<<"行数:"<<l<<endl;         
        fin.close();
 } 
main

实验总结:这次实验做了很久,尤其是part 3的部分,我简直是无从下手,真的是很头痛。part 3的第一题我一开始截图就截少了,又重新运行,结果发现程序有错的地方,又花了好长时间才完成。通过这次实验,发现自己还是会有很多问题,还是要花很多时间,效率不高。

    

猜你喜欢

转载自www.cnblogs.com/wjh1022/p/11028328.html