标签:cst ota clu use date rand 包含 cti close
1
合并文件,添加语句
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    string filename;
    cout << "输入打开的文件: ";
    cin >> filename;
    ofstream fout;
    fout.open(filename,ios_base::app);
    if(!fout.is_open()){
        cerr << "fail to open " << filename << endl;
        system("pause");
        exit(0);
    }
    fout << endl << "merge successfully" << endl;
    return 0;
}

2
main.cpp
#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 filename1;
    int num, totalline = 0;
    cout << "输入名单列表文件名:";
    cin >> filename1;
    cout << "输入随机抽点人数:";
    cin >> num;
    fin.open(filename1, ios_base::in);
    if (!fin.is_open()) { 
        cerr << "fail to open file " << filename1 << endl;
        system("pause");
        exit(0);
    }
    string temp;
    string a[100];  
    if (fin) {
        while (getline(fin, temp)) {
            a[totalline++] = temp;    
        }
        fin.close();
        
    }   
    string filename;
    filename = getCurrentDate()+".txt";
    cout << "new file name is "<<filename << endl;
    srand(std::time(0));
    for (int i = 0;i < num;i++) {
        
        int line;
        int random = rand();
        line = rand()%totalline ;
        cout << a[line] << endl;
        fout.open(filename, ios_base::app);
        fout << a[line] << endl;
        fout.close();
    }
    system("pause");    
    return 0;
}
utils.h
//这个头文件里包含了可用工具函数的声明
#include <string>
using std::string;
// 函数声明
// 返回当前系统时间,格式诸如20190607
string getCurrentDate();
utils.cpp
#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));
}

3
main.cpp
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    int i = 0, zifu = 0, danci = 0, hangshu = 1;
    char ch;
    char s[100];
    string filename;
    ifstream file;
    cout << "输入要统计的英文文本文件名:";
    cin >> filename;
    file.open(filename);
    while ((ch = file.get()) != EOF)
    {
        s[i] = ch;
        if (ch != ‘\n‘)
            zifu += 1;
        else if (ch == ‘\n‘)
            hangshu += 1;
        i++;
    }
    for (i = 0; i < 100; i++)
    {
        if (s[i] != ‘ ‘&&s[i] != ‘,‘&&s[i]!=‘.‘&&s[i + 1] == ‘ ‘)
            danci += 1;
        else if (s[i] != ‘ ‘&&s[i] != ‘.‘&&s[i] != ‘,‘&&s[i + 1] == ‘.‘)
            danci += 1;
        else if (s[i] != ‘,‘&&s[i] != ‘ ‘&&s[i]!=‘.‘&&s[i + 1] == ‘,‘)
            danci += 1;
    }
    cout << "字符数:" << zifu << endl;
    cout << "单词数:" << danci << endl;
    cout << "行数:" << hangshu << endl;
    system("pause");
    return 0;
}

标签:cst ota clu use date rand 包含 cti close
原文地址:https://www.cnblogs.com/csc13813017371/p/11027397.html