博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++(实验六)
阅读量:7218 次
发布时间:2019-06-29

本文共 4588 字,大约阅读时间需要 15 分钟。

Part  1

1. 合并两个文件到新文件中。文件名均从键盘输入。 运行程序,结合运行结果及源码中注释,理解和体会文件I/O的方法。 

1 // 合并两个文件内容到一个新文件中。 2 // 文件名均从键盘输入 3 #include 
4 #include
5 #include
6 #include
7 using namespace std; 8 int main() { 9 string filename1, filename2, newfilename;10 cout << "输入要合并的两个文件名: " ;11 cin >> filename1 >> filename2;12 cout << "输入合并后新文件名: " ;13 cin >> newfilename;14 ofstream fout; // 输出文件流对象15 ifstream fin; // 输入文件流对象16 fin.open(filename1); // 将输入文件流对象fin与文件filename1建立关联17 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出18 cerr << "fail to open file " << filename1 << endl;19 system("pause");20 exit(0);21 }22 fout.open(newfilename); // 将输出文件流对象fout与文件newfilename建立关联23 if(!fin.is_open()) { // 如果创建/打开文件失败,输出错误提示信息并退出24 25 cerr << "fail to open file " << newfilename << endl;26 system("pause");27 exit(0);28 }29 char ch;30 // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中31 while(fin.get(ch))32 fout << ch;33 fin.close(); // 关闭文件输入流对象fin与文件filename1的关联34 fout << endl; // 向文件输出流对象fout中插入换行35 fin.open(filename2); // 将输入文件流对象fin与文件filename2建立关联36 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出37 cerr << "fail to open file " << filename2 << endl;38 system("pause");39 exit(0);40 }41 // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中42 while(fin.get(ch))43 fout << ch;44 fin.close(); // 关闭文件输入流对象fin与文件filename2的关联45 fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联46 system("pause");47 return 0;48 }
main.cpp

运行截图

 

Part  2

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

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 int main() { 7 ofstream fout; 8 fout.open("3.txt",ios_base::app); 9 if(!fout.is_open()) { 10 cerr << "fail to open file 3.txt "<< endl;11 system("pause");12 exit(0);13 }14 fout <
<< "merge successfully."<< endl; 15 fout.close();16 system("pause");17 return 0;18 }
main.cpp

运行截图

 

Part  3(待完善)

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

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include "utils.h" 7 using namespace std; 8 9 int main() {10 int i,j;11 char s[83][100];12 string filename,newfilename;13 cout << "请输入文件名: " ;14 cin >> filename;15 cout << "请输入抽点的人数: " ;16 cin >> n;17 newfilename = getCurrentDate();18 ofstream fout;19 ifstream fin;20 srand((unsigned)time(NULL));21 fout.open(newfilename);22 if(!fout.is_open()) {23 cerr << "fail to open " << filename << endl;24 system("pause");25 exit(0);26 }27 fin.open(filename);28 if(!fin.is_open()) {29 cerr << "fail to open file " << filename << endl;30 system("pause");31 exit(0);32 } for (i = 0; i < 83; i++) {33 for (j = 0; j < 100; j++) {34 ch = fin.get();35 s[i][j] = ch;36 if (ch == '\n')37 {38 break;39 }40 41 }42 }43 fin.close();44 fout.open(getCurrentDate());45 for (i = 0; i < n; i++) {46 a = rand() % 83;47 cout << s[a];48 fout << s[a];49 }50 51 fout.close();52 system("pause");53 return 0;54 }
main.cpp

*这道题utils.h一直显示

 

Part  4

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

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 int main(){ 8 string filename; 9 int n=0,l=1,w=1;10 char ch;11 cout<<"输入要统计的英文文本文件名:";12 cin>>filename;13 ifstream fin;14 fin.open(filename); 15 if(!fin.is_open()) { 16 cerr << "fail to open file " << filename << endl;17 exit(0); 18 } 19 while(fin.get(ch)) 20 {21 if(ch!='\n')22 {23 n++;24 if(ch==' ')25 w++;26 }27 else28 {29 w++;30 l++;31 }32 } 33 cout<<"字符数: "<
<
main.cpp

运行截图

 

 

实验总结:

对流类库与I/O有了新的认识,但在实验中意识到对这部分的知识还是薄弱的,需要其他同学的帮助

对于Xcode与Windows某些兼容性还需完善,

 

评论链接

https://www.cnblogs.com/agsjg/p/10970474.html

https://www.cnblogs.com/mxueyyqx/p/10963449.html

https://www.cnblogs.com/aiwenzhuo/p/10970940.html

转载于:https://www.cnblogs.com/dadadacy/p/11044618.html

你可能感兴趣的文章
遗传算法
查看>>
将web网站移动化
查看>>
Application-Session-Cookie
查看>>
Perl的多进程框架(watcher-worker)
查看>>
phpMyAdmin 后台拿webshell
查看>>
Linux 关机 休眠, 关闭移动设备自动挂载 命令
查看>>
Html唤起手机APP,如果有就唤起,如果没有就跳到下载页。
查看>>
Java中File类如何扫描磁盘所有文件包括子目录及子目录文件
查看>>
VC++ 限制窗口的大小范围的方法
查看>>
结对开发-返回一个整数数组中最大子数组的和(首尾相接版)
查看>>
meanshift-聚类
查看>>
不要if else的编程
查看>>
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>
大型分布式网站架构技术总结
查看>>
矩阵求导与投影梯度相关问题
查看>>