实验6 流类库与I/O
- Part2 基础练习
使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // 合并两个文件内容到一个新文件中。 2 // 文件名均从键盘输入 3 4 #include <iostream> 5 #include <fstream> 6 #include <string> 7 #include <cstdlib> 8 using namespace std; 9 10 int main() { 11 string filename1, filename2, newfilename; 12 13 cout << "输入要合并的两个文件名: " ; 14 cin >> filename1 >> filename2; 15 cout << "输入合并后新文件名: " ; 16 cin >> newfilename; 17 18 ofstream fout; // 输出文件流对象 19 ifstream fin; // 输入文件流对象 20 21 22 fin.open(filename1); // 将输入文件流对象fin与文件filename1建立关联 23 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出 24 cerr << "fail to open file " << filename1 << endl; 25 system("pause"); 26 exit(0); 27 } 28 29 fout.open(newfilename); // 将输出文件流对象fout与文件newfilename建立关联 30 if(!fin.is_open()) { // 如果创建/打开文件失败,输出错误提示信息并退出 31 cerr << "fail to open file " << newfilename << endl; 32 system("pause"); 33 exit(0); 34 } 35 36 char ch; 37 38 // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 39 while(fin.get(ch)) 40 fout << ch; 41 42 fin.close(); // 关闭文件输入流对象fin与文件filename1的关联 43 44 fout << endl; // 向文件输出流对象fout中插入换行 45 46 47 fin.open(filename2); // 将输入文件流对象fin与文件filename2建立关联 48 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出 49 cerr << "fail to open file " << filename2 << endl; 50 system("pause"); 51 exit(0); 52 } 53 54 // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 55 while(fin.get(ch)) 56 fout << ch; 57 58 fin.close(); // 关闭文件输入流对象fin与文件filename2的关联 59 fout << endl; 60 fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联 61 62 63 ofstream out; 64 out.open("3.txt",ios::app); 65 out<<"merge successfully."; 66 out.close(); 67 68 system("pause"); 69 return 0; 70 }
运行结果:
- Part3 应用编程实践
1、已知名单列表文件list.txt。编写一个应用程序,实现从名单中随机抽点n位同学(n由键盘输入),在屏幕上显 示结果,同时也将结果写入文本文件,文件名自动读取当天系统日期,如20190611.txt。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include "utils.h" 4 #include <fstream> 5 #include <cstdlib> 6 #include <ctime> 7 #include <vector> 8 using namespace std; 9 10 int main() { 11 string filename,filename1; 12 int n,i,x; 13 filename = getCurrentDate(); 14 filename+=".txt"; 15 cout << "输入名单列表文件名: " ; 16 cin >> filename1; 17 cout << "输入随机抽点人数: " ; 18 cin >> n; 19 cout << "随机抽点中…… "<<endl; 20 21 ofstream fout; 22 ifstream fin; 23 24 fin.open(filename1); 25 if(!fin.is_open()) { 26 cerr << "fail to open file " << filename1 << endl; 27 exit(0); 28 } 29 30 vector<string>infor; 31 string t; 32 while(!fin.eof()) 33 {getline(fin,t); 34 infor.push_back(t);} 35 36 37 fout.open(filename); 38 if(!fin.is_open()) { 39 cerr << "fail to open file " << filename << endl; 40 exit(0); 41 } 42 43 srand((int) time (NULL)); 44 for(i=1;i<=n;i++) 45 {x=rand()%(83-1+1)+1; 46 fout<<infor[x]<<endl; 47 cout<<infor[x]<<endl; 48 } 49 50 fin.close(); 51 fout.close(); 52 53 return 0; 54 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "utils.h" 2 #include <ctime> 3 using std::string; 4 5 const int SIZE = 20; 6 7 // 函数功能描述:返回当前系统日期 8 // 参数描述:无参数 9 // 返回值描述:以string类型返回系统当前日期,格式诸如20190611 10 string getCurrentDate() { 11 12 time_t now = time(0); // 获取当前系统日历时间 13 14 struct tm *local_time = localtime(&now); // 把系统日历时间转换为当地时间 15 16 char date[SIZE]; 17 18 strftime(date, SIZE, "%Y%m%d", local_time); 19 20 return (string(date)); 21 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //这个头文件里包含了可用工具函数的声明 2 3 #include <string> 4 using std::string; 5 6 // 函数声明 7 // 返回当前系统时间,格式诸如20190611 8 string getCurrentDate();
我没保存这份名单txt的截图,因为我是霉粉,不忍心删掉这张图片(。•́︿•̀。)
运行结果:
2、编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <cstdlib> 5 using namespace std; 6 7 int main() { 8 string filename; 9 10 cout << "输入要统计的英文文本文件名: "; 11 cin >> filename; 12 13 ifstream fin; 14 15 fin.open(filename); 16 if(!fin.is_open()) { 17 cerr << "fail to open file " << filename << endl; 18 exit(0); 19 } 20 21 char ch; 22 int charn=0,wordn=0,linen=0,last=0; 23 while(fin.get(ch)) 24 {charn++; 25 if(ch=='\n') 26 {linen++;charn--;} 27 if(ch==' '||ch=='\t'||ch=='\n') 28 last=0; 29 else if(last==0) 30 {wordn++;last=1;} 31 } 32 if(ch!='\n') 33 linen++; 34 35 cout<<"字符数:"<<charn<<endl 36 <<"单词数:"<<wordn<<endl 37 <<"行数:"<<linen<<endl; 38 39 fin.close(); 40 return 0; 41 } 42
运行结果:
实验总结:
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