Part 1
1. 合并两个文件到新文件中。文件名均从键盘输入。 运行程序,结合运行结果及源码中注释,理解和体会文件I/O的方法。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 // 合并两个文件内容到一个新文件中。 2 // 文件名均从键盘输入 3 #include4 #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 }
运行截图
Part 2
使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #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 }
运行截图
Part 3(待完善)
已知名单列表文件list.txt。编写一个应用程序,实现从名单中随机抽点n位同学(n由键盘输入),在屏幕上显 示结果,同时也将结果写入文本文件,文件名自动读取当天系统日期,如20190611.txt。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #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 }
*这道题utils.h一直显示
Part 4
编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #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<<"字符数: "< <
运行截图
实验总结:
对流类库与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