码迷,mamicode.com
首页 > 其他好文 > 详细

实验6

时间:2019-06-16 15:45:25      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:获取   pre   ret   pause   etc   pen   gif   word   str   

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

#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;
}
View Code

技术图片

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

#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的关联
    
    ofstream fapp;
    fapp.open(newfilename,ios::app);
    fapp<<endl<<"merge successfully.";
    fapp.close();
    system("pause");

    return 0;
}
View Code

技术图片

技术图片
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib> 
#include <time.h>
#include "utils.h"

using namespace std;

int main() {

    string filename, listfilename, name;

    filename = getCurrentDate();

    cout << filename << endl;
    int n, num[100], m, 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
技术图片
#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

技术图片

技术图片
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
using namespace std;
int main(){
    string filename;
    cout << "输入要统计的英文文本文件名:";
    cin>>filename;
    ifstream fin;
    fin.open(filename);
    if(!fin.is_open()){
        cerr << "fail to open " << filename << endl;
        system("pause");
        exit(0);
    } 
    int ch=0,word=0,line=0;
    char ch1;
    int flag=1; 
    while(fin.get(ch1)){
        if(ch1== ){
            ch++;
            if(flag==0)
            flag=1;
        }
        else if(ch1==\n){
            line++;
            if(flag==0)
            flag=1;
        }
        else if(flag&&((ch1>=a&&ch1<=z)||(ch1>=A&&ch1<=Z))){
            flag=0;
            ch++;
            word++;
        }
        else
        ch++;
        
    }
    if(ch1!=\n)
    line++;
    
    cout << "字符数:" << ch << endl;
    cout << "单词数: " << word << endl;
    cout << "行数: " << line << endl;
    fin.close();
    return 0;
}
View Code

技术图片

实验6

标签:获取   pre   ret   pause   etc   pen   gif   word   str   

原文地址:https://www.cnblogs.com/Tiger-Hu22/p/11031397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!