实验6

1.合并两个文件到新文件中。

#include 
#include    
#include <string>
#include 
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的关联

    ofstream fapp;
    fapp.open(newfilename,ios::app);
    fapp<"merge successfully.";
    fapp.close();
    system("pause");
    
    return 0;
} 
main.cpp

实验6_第1张图片

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

#include "utils.h"
#include 
using std::string;

const int SIZE = 20;

// 函数功能描述:返回当前系统日期 
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611 
string getCurrentDate() {
    
    time_t now = time(0);  // 获取当前系统日历时间
    
    struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    
    char date[SIZE];

    strftime(date, SIZE, "%Y%m%d", local_time);
    
    return (string(date));
} 
utils.cpp
//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();
utils.h
#include 
#include <string>
#include 
#include  
#include 
#include "utils.h"

using namespace std;

int main() {

    string filename, listfilename, name;

    filename = getCurrentDate();

    cout << filename << endl;
    int num[100], m,n, i = 1, j;
    cout << "输入名单列表文件名:";
    cin >> listfilename;
    cout << "输入随机抽点人数: ";
    cin >> n;
    ifstream fin;
    ofstream fout;
    fin.open(listfilename);
    if (!fin.is_open()) {
        cerr << "fail to open " << listfilename << endl;
        system("pause");
        exit(0);
    }
    fout.open(filename, ios_base::app);
    if (!fout.is_open()) {
        cerr << "fail to open " << filename << endl;
        system("pause");
        exit(0);
    }
    srand(time(0));
    while (n--) {
        m = 1 + rand() % 83;
        num[i] =m;
        i++;
        fin.clear();
        fin.seekg(0);
        for (j = 1; j <= m; j++)
            getline(fin, name);
        fout << name << endl;
        cout << name;
        cout<< endl;
    }
    fout << endl;
    fin.close();
    fout.close();
    system("pause");
    return 0;
}
main.cpp

实验6_第2张图片

实验6_第3张图片

3.统计单词数

#include 
#include 
#include 
#include 
using namespace std;

int main(){
    string txtname,line0;
    char x[1000];
    cout<<"输入要统计的英文文本文件名:";
    cin>>txtname;
    ifstream fin;
    int n=0,i=1,j=1;
    char c;
    fin.open(txtname);
    if(!fin.is_open()){
        cerr<<"fail to open file "<endl;
        exit(0);
    }
    while(fin.get(c)){
        if(c!='\n'){
            n++;
            if(c==' ')
            j++;
        }
        else
        {j++;
         i++;
        }
    }
    cout<<"字符数:"<endl;
    cout<<"单词数:"<endl;
    cout<<"行数:"<endl;
    return 0;
} 
main.cpp

实验6_第4张图片

总结:就做第二第三题时,自己明明把几个文件都放在一个文件夹里面,不知道是程序错误还是什么原因,一直fail to open,做到崩溃。然后做第二题时自己一时不知道怎么做,看了同学的代码,稍稍理解。反正做起来都不太顺。

 

你可能感兴趣的:(实验6)