part 2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 合并两个文件内容到一个新文件中。 // 文件名均从键盘输入 #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { char filename1[10], filename2[10], newfilename[10]; cout << "输入要合并的两个文件名: " ; gets(filename1); gets(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(!fin.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<<"\nmerge successfully."<<endl; fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联 system("pause"); return 0; }
part 3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <string> using std::string; // º¯ÊýÉùÃ÷ // ·µ»Øµ±Ç°ÏµÍ³Ê±¼ä£¬¸ñʽÖîÈç20190611 string getCurrentDate();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <string> #include<fstream> #include<ctime> #include<cstdlib> #include "utils.h" using namespace std; int main() { cout<<"ÊäÈëÃûµ¥ÁбíÎļþÃû:"; string filename1,filename2; cin>>filename1; filename2=getCurrentDate(); cout<<"ÊäÈëËæ»ú³éµãÈËÊý:"; int n; cin>>n; cout<<"Ëæ»ú³éµãÖÐ..."<<endl; ofstream fout; fout.open(filename2); ifstream fin; fin.open(filename1); string k[83]; string s; for(int i=0;i<83;i++) getline(fin,k[i]); fin.close(); for(int i=0;i<n;i++) { ifstream fin; fin.open(filename1); int a; do{ srand(time(NULL)); int w=rand(); a=rand()%83+1; }while(k[a]=="0"); fout<<k[a]; cout<<k[a]<<endl; fout<<endl; k[a]="0"; fin.close(); } fout.close(); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "utils.h" #include <ctime> using std::string; const int SIZE = 20; string getCurrentDate() { time_t now = time(0); struct tm *local_time = localtime(&now); char date[SIZE]; strftime(date, SIZE, "%Y%m%d", local_time); return (string(date)); }
这部分内容还不熟,于是询问了同学的做法,虽然五花八门,但是我觉得都仍有改进的空间