1. #include<iostream> 
  2. #include<fstream> 
  3. #include<string> 
  4. using namespace std;  
  5. struct Data  
  6. {  
  7.     string name;  
  8.     int age;  
  9.       
  10. };  
  11. class file  
  12. {  
  13. public:  
  14.     file(){};  
  15.     ~file(){};  
  16.     void input_file(const string filename);  
  17.     void output_file(const string filename);  
  18.  
  19.  //friend   istream& operator>>(istream& in,Data&data)  
  20.  //{  
  21.     // in>>data.name>>data.age;  
  22.  //};  
  23.  //friend ostream& operator<<(ostream&out,Data&data)  
  24.  //{  
  25.     // cout<<data.name<<"  "<<data.age;  
  26.  //};  
  27. private:  
  28. };  
  29. istream& operator>>(istream& in,Data&data)  
  30.  {  
  31.     return in>>data.name>>data.age;  
  32.  };  
  33.  ostream& operator<<(ostream&out,Data&data)  
  34.  {  
  35.      return cout<<data.name<<"  "<<data.age;  
  36.  };  
  37. void file ::output_file(const string filename)  
  38. {  
  39.     Data data;  
  40.     ofstream ofs_file;  
  41.     cout<<"please input a string and a int for exemple  hello 12";  
  42.     ofs_file.close();  
  43.     ofs_file.clear();  
  44.     ofs_file.open(filename.c_str(),ios_base::app);  
  45.     while(cin>>data)  
  46.     {  
  47.         ofs_file<<data<<endl;  
  48.         ofs_file.clear();  
  49.         ofs_file<<data.name<<" "<<data.age<<endl;  
  50.           
  51.  
  52.     }   
  53.     ofs_file.close();  
  54.  
  55. }  
  56. void file::input_file(const string filename)  
  57. {  
  58.     Data data;  
  59.     ifstream ifs_file;  
  60.     ifs_file.close();  
  61.     ifs_file.clear();  
  62.     ifs_file.open(filename.c_str());  
  63.     while(ifs_file>>data)  
  64.     {  
  65.     cout<<data<<endl;}  
  66.     ifs_file.close();  
  67.  
  68. }  
  69.  
  70.  int main()  
  71.  {  
  72.      file f;  
  73.      string filename("C:\\Users\\Administrator\\Desktop\\Keygen\\word2.txt");  
  74.     // f.output_file(filename.c_str());  
  75.        
  76.      f.input_file(filename.c_str());  
  77.       
  78.  
  79.      return 0;  
  80.  }