• Part2 基础练习

使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "

 // 合并两个文件内容到一个新文件中。
// 文件名均从键盘输入 #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();
} fout.open(newfilename); // 将输出文件流对象fout与文件newfilename建立关联
if(!fin.is_open()) { // 如果创建/打开文件失败,输出错误提示信息并退出
cerr << "fail to open file " << newfilename << endl;
system("pause");
exit();
} 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();
} // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
while(fin.get(ch))
fout << ch; fin.close(); // 关闭文件输入流对象fin与文件filename2的关联
fout << endl;
fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联 ofstream out;
out.open("3.txt",ios::app);
out<<"merge successfully.";
out.close(); system("pause");
return ;
}

main

运行结果:

  •  Part3 应用编程实践

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

 #include <iostream>
#include <string>
#include "utils.h"
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std; int main() {
string filename,filename1;
int n,i,x;
filename = getCurrentDate();
filename+=".txt";
cout << "输入名单列表文件名: " ;
cin >> filename1;
cout << "输入随机抽点人数: " ;
cin >> n;
cout << "随机抽点中…… "<<endl; ofstream fout;
ifstream fin; fin.open(filename1);
if(!fin.is_open()) {
cerr << "fail to open file " << filename1 << endl;
exit();
} vector<string>infor;
string t;
while(!fin.eof())
{getline(fin,t);
infor.push_back(t);} fout.open(filename);
if(!fin.is_open()) {
cerr << "fail to open file " << filename << endl;
exit();
} srand((int) time (NULL));
for(i=;i<=n;i++)
{x=rand()%(-+)+;
fout<<infor[x]<<endl;
cout<<infor[x]<<endl;
} fin.close();
fout.close(); return ;
}

main

 #include "utils.h"
#include <ctime>
using std::string; const int SIZE = ; // 函数功能描述:返回当前系统日期
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611
string getCurrentDate() { time_t now = time(); // 获取当前系统日历时间 struct tm *local_time = localtime(&now); // 把系统日历时间转换为当地时间 char date[SIZE]; strftime(date, SIZE, "%Y%m%d", local_time); return (string(date));
}

utils.cpp

 //这个头文件里包含了可用工具函数的声明 

 #include <string>
using std::string; // 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();

utils.h

   我没保存这份名单txt的截图,因为我是霉粉,不忍心删掉这张图片(。•́︿•̀。)

运行结果:

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

 #include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std; int main() {
string filename; cout << "输入要统计的英文文本文件名: ";
cin >> filename; ifstream fin; fin.open(filename);
if(!fin.is_open()) {
cerr << "fail to open file " << filename << endl;
exit();
} char ch;
int charn=,wordn=,linen=,last=;
while(fin.get(ch))
{charn++;
if(ch=='\n')
{linen++;charn--;}
if(ch==' '||ch=='\t'||ch=='\n')
last=;
else if(last==)
{wordn++;last=;}
}
if(ch!='\n')
linen++; cout<<"字符数:"<<charn<<endl
<<"单词数:"<<wordn<<endl
<<"行数:"<<linen<<endl; fin.close();
return ;
}

main

运行结果:

实验总结:

1、感谢大佬分享的链接,让我在Part3的第一题,能够顺利做出来

循环条件中,判定,只要文件流没有到达末尾,循环始终进行,并且将每一行的数据赋给一个vector

我再分享一次(#^.^#)   https://blog.csdn.net/isbnhao/article/details/8055359

2、随机函数是参考了以前C语言老师讲滴版本

3、巩固了使用文件流类实现文件I/O的步骤和方法,以及常用的一些成员函数或普通函数

评论:

1、https://www.cnblogs.com/ggwdcs/p/11030950.html

2、https://www.cnblogs.com/wyf-blogs/p/11029005.html

3、https://www.cnblogs.com/csc13813017371/p/11027397.html

最新文章

  1. HTTP学习一:HTTP基础知识
  2. Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!
  3. Sublime Text3快捷键实用总结
  4. 20145113 实验二 Java面向对象程序设计
  5. B2车
  6. [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】
  7. 【原创】java中的父进程子进程 —— 坑爹的java Runtime.getRuntime().exec
  8. js实现分页
  9. windows系统中,在当前目录下打开cmd命令行的两种方法
  10. PHP require php &gt; 5.3.0
  11. 浅析Linux服务器集群系统技术
  12. 转: linux centos7 下安装maven
  13. c 语言笔记 数组1
  14. vivado 连接不上板子 There is no current hw_target
  15. 在同一台电脑安装python 2 和3,并且怎样安装各自的pip和模块
  16. Java之JDBC操作
  17. Codeforces D - High Load
  18. BZOJ4756:[USACO]Promotion Counting(线段树合并)
  19. java语言复制数组的四种方法
  20. 一、Mosquitto 介绍&amp;安装

热门文章

  1. Mavlink消息包解析
  2. Linux让Apache支持中文URL图片/文件名
  3. Jsoup进阶选择器
  4. HDU5269 字典树
  5. spa 小程序的研发随笔 (2) --- 预编译
  6. PHP:is_string()字符串函数
  7. 【HDU1542】Atlantis (扫描线的经典运用)
  8. CopyOnWriteArrayList分析——能解决什么问题
  9. HTML 5新元素和CSS
  10. latex目录标题常用宏包说明与示例