当前位置: 首页 > news >正文

实验6 流类库与I/O

part1

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

part2

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

 

 part3

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

#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);
    char date[SIZE];

    strftime(date, SIZE, "%Y%m%d", &now_time);

    return (string(date));
}
utils.cpp
//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190607
string getCurrentDate();
utils.h

 

#include <iostream>
#include <string>
#include <cstdlib>
#include<fstream>
#include<time.h>
#include "utils.h"

using namespace std;

int main() {

    string filename;
    int n, m = 0, i;

    ofstream fout;
    ifstream fin;

    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[m++] = s;
        }
        fin.close();
    }
    string filename1;
    filename = getCurrentDate();
    cout << "随机抽点中..." << filename1 << endl;
    srand(time(0));
    for (i = 1;i <= n;i++) {
        int num;
        int r = rand();
        num = rand() % m;
        cout << a[num] << endl;
        fout.open(filename, ios_base::app);
        fout << a[num] << endl;
        fout.close();
    }

    system("pause");

    return 0;
}
main.cpp

 

 

 

 

 

 

2. 编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。

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

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

总结:在对文件进行操作时,要把文件放在相应程序的文件夹里。可能是软件版本问题,做出来总是fail to open。相较于之前的程序,简单了些,但是总是出错,还是对基础知识运用不够熟练,做起来没那么顺。

转载于:https://www.cnblogs.com/jackyayue/p/11040551.html

相关文章:

  • Python day 44 :数据库的存储引擎/索引/权限管理
  • vsphere6.7-虚拟机与ESXI时间同步
  • 结合案例深入解析迭代器模式
  • laravel5.5入门-安装和认证
  • 没事干 回忆下快排
  • 并发
  • 微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate
  • SmartGit 合并分支,图文
  • mysql创建用户
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • linux内核代码基础篇(一)一目录架构说明
  • 基于 Egg.js 框架的 Node.js 服务构建之用户管理设计
  • WPF — Grid布局中行的高度和列的高度值定义的三种形式
  • 判断两个字符串是否互为变形词
  • Blob
  • Google 是如何开发 Web 框架的
  • JavaScript-如何实现克隆(clone)函数
  • 【RocksDB】TransactionDB源码分析
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • ➹使用webpack配置多页面应用(MPA)
  • C++类的相互关联
  • Java 23种设计模式 之单例模式 7种实现方式
  • java小心机(3)| 浅析finalize()
  • python 装饰器(一)
  • Redis中的lru算法实现
  • Redux 中间件分析
  • Vue全家桶实现一个Web App
  • 前端技术周刊 2019-02-11 Serverless
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • Prometheus VS InfluxDB
  • 阿里云ACE认证学习知识点梳理
  • 阿里云重庆大学大数据训练营落地分享
  • 国内开源镜像站点
  • #android不同版本废弃api,新api。
  • (1)bark-ml
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (js)循环条件满足时终止循环
  • (翻译)terry crowley: 写给程序员
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (一)基于IDEA的JAVA基础12
  • (一)认识微服务
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • (转)nsfocus-绿盟科技笔试题目
  • (转)程序员技术练级攻略
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .NET Framework杂记
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • @德人合科技——天锐绿盾 | 图纸加密软件有哪些功能呢?
  • [ solr入门 ] - 利用solrJ进行检索
  • [2016.7.test1] T2 偷天换日 [codevs 1163 访问艺术馆(类似)]
  • [AIGC] 使用Curl进行网络请求的常见用法
  • [android] 练习PopupWindow实现对话框
  • [ARM]ldr 和 adr 伪指令的区别