实验6
part 2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 合并两个文件内容到一个新文件中。 // 文件名均从键盘输入 #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(0); } fout.open(newfilename); // 将输出文件流对象fout与文件newfilename建立关联 if(!fout.is_open()) { // 如果创建/打开文件失败,输出错误提示信息并退出 cerr << "fail to open file " << newfilename << endl; system("pause"); exit(0); } 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(0); } // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 while(fin.get(ch)) fout << ch; fin.close(); // 关闭文件输入流对象fin与文件filename2的关联 fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联 ofstream fapp; fapp.open(newfilename,ios::app); if(!fapp.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出 cerr << "fail to open file 3.txt "<< endl; system("pause"); exit(0); } fapp<<endl<<"merge successfully."; fapp.close(); system("pause"); return 0; }
part 3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<fstream> #include<string> #include<vector> #include<cstdlib> #include "utils.cpp" using namespace std; int main() { string filename; filename = getCurrentDate(); string fn,temp; int cnt,recordcnt; ifstream fin; ofstream fout; vector<string> records; cout<<"输入名单列表文件名:"; cin>>fn; fin.open(fn); if(!fin.is_open()) { cerr << "fail to open file"<<fn<< endl; system("pause"); exit(0); } while(getline(fin,temp))//读取 records.push_back(temp); fin.close(); recordcnt=records.size(); cout<<"输入随机抽点人数:"; cin>>cnt; if(cnt>recordcnt) { cerr<<"input number is too big"<<endl; system("pause"); exit(0); } fout.open(filename); cout<<"随机抽点中..."<<endl; int hit; srand((int)time(NULL)); for(int i=0;i<cnt;i++) { hit=rand()%(recordcnt); cout<<records[hit]<<endl; fout<<records[hit]<<endl; recordcnt--; records.erase(records.begin()+hit); } fout.close(); system("pause"); return 0; }
utils.cpp
part 4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string fn,temp; char tch; ifstream fin; int charcnt,wordcnt,linecnt; cout<<"输入要统计的英文文本文件名:"; cin>>fn; fin.open(fn); if(!fin.is_open()) { cerr << "fail to open file"<<fn<< endl; system("pause"); exit(0); } //统计字符 charcnt = 0; while(fin.get(tch)) charcnt++; charcnt--; //统计单词 wordcnt=0; fin.close(); fin.open(fn); while(fin>>temp) wordcnt++; //统计行 linecnt=0; fin.close(); fin.open(fn) ; while(getline(fin,temp)) linecnt++; fin.close(); cout<<"字符数:"<<charcnt<<endl; cout<<"单词数:"<<wordcnt<<endl; cout<<"行数:"<<linecnt<<endl; system("pause"); return 0; }
总结:不说了做了半天。