main175.cpp
不知道为什么,文件追加不成功,都是只能更新,知道的麻烦告诉下我
#include <iostream> #include <set> #include <algorithm> #include <string> #include <iterator> #include <fstream> #include <cctype> using namespace std; //由题知道,键值唯一,可以知道,应该用set static void output(string str) { cout<<str<<" "; }; static bool check(ifstream &fcheck,string &str)//检测文件中是否已经存在该名字 { string checkstr; int flag; // int sizestr; int sizecheck; sizestr=str.length(); while(fcheck.good()) { //getline(fcheck,checkstr);//多1个长度 //getline(fcheck,checkstr,'\n');//多1个长度 fcheck>>checkstr;//这样才保证长度正确,应该是因为输入的时候用的是cin>> sizecheck=checkstr.length();//这里的为什么会多1个长度,getline? flag=str.compare(checkstr);//(str==checkstr);//为何总返回-1?? if(flag==0) //忘记了str的是地址,不能直接比较,比较的是地址!! return true; } if(fcheck.eof()) return false; }; static bool writeToSet(ifstream &fwrite,set<string> &s) { string str; while(fwrite.good()) { //getline(fwrite,str);//不要用这个,会把文件最后一个换行读进去 fwrite>>str; s.insert(str); } if(fwrite.eof()) return true; else return false; } /* static bool input(ofstream &fout,const char *filename) { ifstream fcheck; string temp; cout<<"plesase enter your friends name(q to quit):"; cin>>temp; while(temp!="q") { //Mat.insert(Mat.begin(),temp); fcheck.open(filename,ios_base::binary);//流重定位 if(check(fcheck,temp))//如果已经存在 { cout<<"\nYou had already writer it!Enter others name:"<<endl; } else//否则写入 { fout<<temp; fout<<endl; cout<<"\nwrite success!"<<endl; } fout.flush();//刷新流,写入 fcheck.close(); cout<<"\nMat ,plesase enter your friends name continue(q to quit):"; cin>>temp; } while(cin.get()!='\n') continue; return true; } */ static bool save(set<string> &s) { //ofstream fout("All.dat",ios_base::binary||ios_base::app);//追加模式//为何不是直接在文件尾部追加呢? ofstream fout("All.dat",ios_base::ate||ios_base::app); if(!fout.is_open()) { cout<<"Open All.dat error!"<<endl; return false; } set<string>::iterator it; for(it=s.begin();it!=s.end();++it) { fout<<*it; //fout<<" "; fout<<endl;//<<忽略end?? fout.flush(); } fout.close(); return true; } void main175() { ofstream foutM("Mat.dat",ios_base::binary||ios_base::app);//追加模式 ofstream foutP("Pat.dat",ios_base::binary||ios_base::app);//需文件已存在,否则无法以追加打开 ifstream fcheck; ifstream fwrite; if(!foutM.is_open()) cout<<" Mat.dat open failure!"<<endl; if(!foutP.is_open()) cout<<" Pat.dat open failure!"<<endl; //input(foutM,"Mat.dat"); //初始化Mat.dat文件 string temp; cout<<"Mat ,plesase enter your friends name(q to quit):"; cin>>temp; while(temp!="q") { //Mat.insert(Mat.begin(),temp); fcheck.open("Mat.dat",ios_base::binary);//流重定位 if(check(fcheck,temp))//如果已经存在 { cout<<"\nYou had already writer it!Enter others name:"<<endl; } else//否则写入 { foutM<<temp; foutM<<endl; cout<<"\nwrite success!"<<endl; } foutM.flush();//刷新流,写入 fcheck.close(); cout<<"\nMat ,plesase enter your friends name continue(q to quit):"; cin>>temp; } while(cin.get()!='\n') continue; set<string> Mat; set<string> Pat; fwrite.open("Mat.dat",ios_base::binary); if(writeToSet(fwrite,Mat)) cout<<"\nwrite to Mat success!"<<endl; else cout<<"Something wrong!"<<endl; fwrite.close(); cout<<"Mat's friend list: "; for_each(Mat.begin(),Mat.end(),output); //Pat.dat cout<<"\nNow ,Pat ,please enter your friend's name:"; cin>>temp; while(temp!="q") { //Mat.insert(Mat.begin(),temp); fcheck.open("Pat.dat",ios_base::binary);//流重定位 if(check(fcheck,temp))//如果已经存在 { cout<<"\nYou had already writer it!Enter others name:"<<endl; } else//否则写入 { foutP<<temp; foutP<<endl; cout<<"\nwrite success!"<<endl; } foutP.flush();//刷新流,写入 fcheck.close(); cout<<"\nMat ,plesase enter your friends name continue(q to quit):"; cin>>temp; } while(cin.get()!='\n') continue; fwrite.open("Pat.dat",ios_base::binary); if(writeToSet(fwrite,Pat)) cout<<"\nwrite to Pat success!"<<endl; else cout<<"Something wrong!"<<endl; fwrite.close(); cout<<"Pat's friend list: "; for_each(Pat.begin(),Pat.end(),output); set<string> All; //transform(Mat.begin(),Mat.end(),All.begin(),[](set<string>::iterator it){return it;});// //set_union(Mat.begin(),Mat.end(),Pat.begin(),Pat.end(),All.begin());//用set_union set_union(Mat.begin(),Mat.end(),Pat.begin(),Pat.end(),insert_iterator<set<string> >(All,All.begin()));//p703 cout<<"\nAll list:"; for_each(All.begin(),All.end(),output); cout<<endl; if(save(All)) cout<<"Save perfer!"<<endl; cin.get(); cin.get(); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————