part 1 验证性实验
合并两个文件. 虽说验证, 但后两个实验均受该代码指导启发.
part 2 文末添加数据
1, 代码
- #include<fstream>
- #include<iostream>
- #include<string>
- using namespace std;
- int main()
- {
- ofstream out("C:\\Users\\lenovo\\Desktop\\ 实验六 \\ 实验六 part1\\ 实验六 part1\\3.txt",ios_base::App);
- if (!out)
- {
- cout <<"Fail to open." << endl;
- return 1;
- }
- out << endl;
- out << "merge successfully";
- out.close();
- return 0;
- }
- part 2
2, 程序截图
3, 知识点
1"打开一个与输出流相关的文件时, 可以指定一个 open_mode 标志, 实现一些特定模式. 用' | '组合多个." 本题需要在文件尾添加数据, 即 "ios_base::app".
2路径写法注意双斜杠, 为了与转义字符相区别.
Part 3
一, 随机抽取
1, 代码
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<ctime>
- #include<sstream>
- #include<Windows.h>
- using namespace std;
- string getdate();
- int main()
- {
- string filename, newfilename;//f 是已存在的名单文件, new 是即将存随机抽取的文件
- int n, i;
- ofstream out;
- ifstream in;
- cout <<"输入名单列表文件名:";
- cin>> filename;
- cout <<"输入随机抽点人数:";
- cin>> n;
- cout <<"随机抽点中..." << endl;
- newfilename = getdate().c_str();// 新建文件命名
- out.open(newfilename);
- if (!out.is_open())
- {
- cout << "Fail to open." << endl;
- return 1;
- }
- for (i = 1; i <= n; i++)
- {
- in.open(filename);
- if (!in.is_open())
- {
- cout << "Fail to open." << endl;
- return 1;
- }
- srand((unsigned)time(NULL));
- int N = rand() % 84;
- string s;
- stringstream ss;
- ss << N;
- ss>> s;
- char c;
- int j=1;
- while (in.get(c))
- {
- if (c == '\n')
- {
- j++;
- if (j == N)
- {
- in.get(c);
- while (c != '\n')
- {
- out <<c;
- in.get(c);
- }
- out << endl;
- break;
- }
- }
- else
- continue;
- }
- in.close();
- Sleep(1000);
- }
- out.close();
- char c;
- ifstream nfile(newfilename);
- while (nfile.get(c))
- cout << c;
- nfile.close();
- return 0;
- }
- const int SIZE1 = 20;
- string getdate()
- {
- time_t time_seconds = time(0);
- struct tm now_time;
- localtime_s(&now_time, &time_seconds); // 使用了更安全的 localtime_s()
- char date[SIZE1];
- strftime(date, SIZE1, "%Y%m%d", &now_time);
- return (string(date));
- }
- part 3.1
2, 程序截图
3, 出现的一些问题
1查找相应标号的同学, 最开始想得简单, 是通过简单对比数字, 后来发现学号里也有数字.(有点儿傻的过分了)后来纠正的一些方法不如数行数更简便, 最终就由数行数来找对应的同学.
2输入流文件打开操作 在 n 次抽取循环的外面, 导致 in.get(c)是接着数的. 例如:
我机智的发现应该把 打开 list 和 打开 20190611 两个操作都放循环里面, 每次循环就可以重新打开重新数. 我还特别机智的立即想到也要在里面关闭两个文件, 结果只保留了最后一个抽取的情况. 例如:
循环哪个地方每次都会清空内容. 翻课本时惊现不久前的笔记, 输出流中隐含指定的模式是 ios_base::out, 当使用该模式打开已存在的文件时, 内容清空. 解决可以用 part 2 中的追加模式.
我是只把 list 打开关闭操作放在循环内.
3每次随机数取的差不多. 例如:
我猜是随机数取的系统时间精度不那么高, 程序运行又很快. 不知道对不对地 人为的 让它每次取随机数前延迟一阵子. 用了 Sleep 函数, 感觉嗯, 慢的过分了.
最终结果就如代码 and 截图.
二, 统计单词
1, 代码
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- int main()
- {
- string filename;
- char c;
- cout <<"输入要统计的英文文本文件名:";
- cin>> filename;
- int a = 0, b = 1, s = 1;//a 字符数 b 单词数 c 行数
- ifstream in;
- in.open(filename);
- if (!in)
- {
- cout <<"Fail to open." << endl;
- return 1;
- }
- while (in.get(c))
- {
- if (c != '\n')
- a++;
- if (c == '\n')
- s++;
- if (c == '' || c =='\n'|| c =='\t')
- {
- in.get(c);
- a++;
- if ((c>= 'a'&&c <= 'z') || (c>= 'A'&&c <= 'Z'))
- {
- b++;
- }
- }
- }
- cout <<"字符数:" << a << endl;
- cout << "单词数:" << b << endl;
- cout << "行数:" << s << endl;
- return 0;
- }
- part 3.2
2, 程序结果截图
3, 一点说明
没有用 getline 等等, 就全一个一个数的, 一次性全数完.
总结
1, 学流类库之前认为插入提取用 <<或者>> 就是刻板规定, 而且学习之初形象理解认为 输出 大概用>>更形象, 所以很多时候还会打反. 在学习流类库的时候也混, 怎么它就是输出流文件, 不是往它里面打字 cin 吗 / 扶额 /. 尤其做完实验才有点儿理解, 明白为什么 cin 后跟提取, cout 后跟插入, 也就是你提取资料是输入, 你把资料筛选出的内容插入到新文件, 又是输出.
2, 做实验总会有做 oj 的错觉, 我会不会少打了回车啊, 好像落了句号啊.../ 叹气 /.
3, 程序错乱或者漏洞可以直接指出, 谢谢.
后继更新.
回顾知识点的时候参考博客 https://blog.csdn.net/w_linux/article/details/72793651
评价
1,
来源: http://www.bubuko.com/infodetail-3089477.html