1 //11-6 2 #include <iostream> 3 #include <fstream> 4 #include <string> 5 using namespace std; 6 class Dog 7 { 8 public: 9 Dog(){} 10 Dog(int age,int wei) 11 { 12 this->m_Age = age; 13 this->m_Wei = wei; 14 } 15 int m_Wei; 16 int m_Age; 17 }; 18 void test() 19 { 20 Dog dog1(10,5); 21 ofstream ofs; 22 ofs.open("dog1.txt",ios::out|ios::binary); 23 ofs<<"dog1的年龄为:"<<dog1.m_Age<<endl; 24 ofs<<"dog1的体重为:"<<dog1.m_Wei<<endl; 25 Dog dog2(dog1.m_Age,dog1.m_Wei); 26 ofs.close(); 27 ///////////// 28 ifstream ifs; 29 ifs.open("dog1.txt",ios::in); 30 if(!ifs.is_open()) 31 { 32 cout<<"打开文件失败"<<endl; 33 return; 34 } 35 ifs.read((char*)&dog2,sizeof(Dog)); 36 cout<<dog1.m_Age<<dog1.m_Wei<<endl; 37 ifs.close(); 38 } 39 int main() 40 { 41 test(); 42 return 0; 43 }
标签:int,Wei,Dog,ofs,2023.5,include,wei From: https://www.cnblogs.com/muzhaodi/p/17378963.html