第一题
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(!fout.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.close(); // 关闭文件输出流对象fout与文件newfilename的关联 60 61 system("pause"); 62 63 64 65 return 0; 66 }
第二题
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(!fout.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.close(); // 关闭文件输出流对象fout与文件newfilename的关联 60 61 system("pause"); 62 63 fout.open(newfilename,ios_base::app); 64 fout << endl; 65 if(!fout.is_open()) 66 { 67 cerr<<"fail to open file "<< newfilename << endl; 68 system("pause"); 69 exit(0); 70 } 71 fout<<"merge successfully"<<endl; 72 73 fout.close(); 74 75 system("pause"); 76 77 78 return 0; 79 }
第三题
(1)
扫描二维码关注公众号,回复:
6451681 查看本文章
1 #include <iostream> 2 #include <string> 3 #include <cstdlib> 4 #include <fstream> 5 #include <time.h> 6 #include "utils.h" 7 8 using namespace std; 9 10 int main() { 11 12 string filename,listfilename,luckydog; 13 int number,i,j,line,record[100]; 14 15 filename = getCurrentDate(); 16 17 cout << filename << endl; 18 19 ofstream fout; 20 ifstream fin; 21 22 cout<<"输入名单列表文件名:"<<endl; 23 cin>>listfilename; 24 25 cout<<"输入抽点人数:"<<endl; 26 cin>>number; 27 28 fin.open(listfilename); 29 if(!fin.is_open()) 30 { 31 cerr<<"fail to open file "<<listfilename<<endl; 32 system("pause"); 33 exit(0); 34 } 35 36 fout.open(filename,ios_base::app); 37 if(!fout.is_open()) 38 { 39 cerr<<"fail to open file "<<filename<<endl; 40 system("pause"); 41 exit(0); 42 } 43 srand(time(NULL)); 44 for(i=1;i<=number;i++) 45 { 46 if(rand()%83==0) 47 line=83; 48 else line=rand()%83; 49 // cout<<line<<" "; 50 if(i==1) 51 record[i]=line; 52 else 53 { 54 for(j=1;j<i;j++) 55 if(record[j]==line) 56 break; 57 if(j!=i) 58 { 59 i--; 60 continue; 61 } 62 else 63 record[i]=line; 64 } 65 // cout<<line<<" "; 66 fin.clear(); 67 fin.seekg(0); 68 for(j=1;j<=line;j++) 69 { 70 getline(fin,luckydog); 71 } 72 fout<<luckydog<<endl; 73 cout<<luckydog<<endl; 74 75 } 76 fout<<endl; 77 fout.close(); 78 fin.close(); 79 80 return 0; 81 }
增加了防止随机数出现重复的情况
1 if(i==1) 2 record[i]=line; 3 else 4 { 5 for(j=1;j<i;j++) 6 if(record[j]==line) 7 break; 8 if(j!=i) 9 { 10 i--; 11 continue; 12 } 13 else 14 record[i]=line;
(2)
1 #include <iostream> 2 #include <fstream> 3 #include <cstdlib> 4 #include <string> 5 using namespace std; 6 int main () 7 { 8 ifstream fin; 9 char ch; 10 int countchar=0,countword=0,countline=0,i,choose,flag=1; 11 string filename,line; 12 cout<<"输入想要统计的英文文本文件名:"<<endl; 13 cin>>filename; 14 fin.open(filename); 15 if(!fin.is_open()) 16 { 17 cout<<"fail to open the file "<<endl; 18 system("pause"); 19 exit(0); 20 } 21 //system("pause"); 22 while(1) 23 { 24 cout<<"选择你想统计的数据类别:"<<endl; 25 cout<<"1.字符数"<<endl 26 <<"2.单词数"<<endl 27 <<"3.行数"<<endl 28 <<"4.退出"<<endl; 29 cout<<"选择:"; 30 cin>>choose; 31 if(choose==1) 32 { 33 while((ch=fin.get())!=EOF) 34 { 35 if(ch=='\n')continue; 36 // cout.put(ch); 37 countchar++; 38 // cout<<' '<<countchar<<' '; 39 } 40 cout<<"字符数:"<<countchar<<endl; 41 fin.clear(); 42 fin.seekg(0); 43 } 44 else if(choose==2) 45 { 46 while((ch=fin.get())!=EOF) 47 { 48 if(flag==1&&(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')) 49 { 50 countword++; 51 flag=0; 52 } 53 if(!(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')) 54 flag=1; 55 } 56 cout<<"单词数:"<<countword<<endl; 57 fin.clear(); 58 fin.seekg(0); 59 } 60 else if(choose==3) 61 { 62 while(getline(fin,line)) 63 { 64 countline++; 65 } 66 cout<<"行数:"<<countline<<endl; 67 } 68 else 69 break; 70 cout<<endl; 71 72 } 73 74 return 0; 75 }
增加了菜单控制选项
总结
(1)统计字符数的时候发现会把'\n'算作一个字符,要注意舍去
(2)文件指针调回到开头可以用fin.clear(); fin.seekg(0);
(3)getline()函数的用法需要熟悉
(4)随机数函数srand(time(NULL))需要放在循环外面,否则随机数都是一样的