- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- struct Data
- {
- string name;
- int age;
- };
- class file
- {
- public:
- file(){};
- ~file(){};
- void input_file(const string filename);
- void output_file(const string filename);
- //friend istream& operator>>(istream& in,Data&data)
- //{
- // in>>data.name>>data.age;
- //};
- //friend ostream& operator<<(ostream&out,Data&data)
- //{
- // cout<<data.name<<" "<<data.age;
- //};
- private:
- };
- istream& operator>>(istream& in,Data&data)
- {
- return in>>data.name>>data.age;
- };
- ostream& operator<<(ostream&out,Data&data)
- {
- return cout<<data.name<<" "<<data.age;
- };
- void file ::output_file(const string filename)
- {
- Data data;
- ofstream ofs_file;
- cout<<"please input a string and a int for exemple hello 12";
- ofs_file.close();
- ofs_file.clear();
- ofs_file.open(filename.c_str(),ios_base::app);
- while(cin>>data)
- {
- ofs_file<<data<<endl;
- ofs_file.clear();
- ofs_file<<data.name<<" "<<data.age<<endl;
- }
- ofs_file.close();
- }
- void file::input_file(const string filename)
- {
- Data data;
- ifstream ifs_file;
- ifs_file.close();
- ifs_file.clear();
- ifs_file.open(filename.c_str());
- while(ifs_file>>data)
- {
- cout<<data<<endl;}
- ifs_file.close();
- }
- int main()
- {
- file f;
- string filename("C:\\Users\\Administrator\\Desktop\\Keygen\\word2.txt");
- // f.output_file(filename.c_str());
- f.input_file(filename.c_str());
- return 0;
- }
转载于:https://blog.51cto.com/jun0213/1058408