C++ 实验六

Part.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.open("3.txt",ios_base::app);
fout<<"\nmerge successfully."<<endl;
fin.close(); // 关闭文件输入流对象fin与文件filename2的关联
fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联
system("pause");
return 0;
}
part2

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

part 3

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

int main(){
    string filename;
    ifstream fin;
    ofstream fout;
    
    int num=0,num1=0,line=0;
    cout<<"输入要统计的英文文本文件名:";
    cin>>filename;
    fin.open(filename);
    if(!fin.is_open()){
        cout<<"fail to open"<<endl;
        exit(0);
    } 
    
    string a;
    string b[10000];
    int i,j;
    while (getline(fin,a)){
        b[line]=a;
        line++;
        j=a.length();
        for(i=0;i<j;i++){
            if(a[i]==' ')
            num1++;
        } 
        num1++;
    }
    for(i=0;i<line;i++){
        j=b[i].length();
        num+=j; 
    }
    fin.close();
    cout<<"字符数:"<<num<<endl;
    cout<<"单词数:"<<num1<<endl;
    cout<<"行数:"<<line<<endl;
    return 0; 
} 
View Code

转载于:https://www.cnblogs.com/lyc1103/p/11041340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值