首页 > 其他 > 详细

实验6

时间:2019-06-18 13:28:40      阅读:96      评论:0      收藏:0      [点我收藏+]

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.cpp

技术分享图片

技术分享图片

技术分享图片

 

2.

技术分享图片
#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 << "\n养不起!养不起!" << endl;
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");

    return 0;
}
ex2.cpp

技术分享图片

3.

技术分享图片
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "utils.h"
using namespace std;
int main() {
    int n,i,j=0,k,s;
    string listname;
    string filename;
    string a[100], t;
    ofstream fout;
    ifstream fin;
    filename = getCurrentDate() + ".txt";
    cout << "输入名单列表文件名:";
    cin >> listname;
    cout << "输入随机抽点人数:";
    cin >> n;
    cout << "随机抽点中..." << endl;
    fin.open(listname, ios_base::in);
    fout.open(filename, ios_base::app);
    while (getline(fin, t)) {
        a[j++] = t;
    }
    fin.close();
    srand(time(0));
    for (i = 0; i < n; i++) {
        k = rand() % j;
        cout << a[k] << endl;
        fout << a[k] << endl;
        for (s = k; s < j - 1; s++)
            a[s] = a[s + 1];
        j--;
    }
    cout << filename << endl;
    system("pause");
    return 0;
}
main.cpp

技术分享图片

 

不熟练,而且大多不会,借鉴了一下大佬的程序,发现自己还是不会。

实验6

原文:https://www.cnblogs.com/qiuxiuh/p/11044537.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!