菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.5.7

 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 }

 

posted on 2023-05-07 10:15  hhmzd233  阅读(7)  评论(0编辑  收藏  举报