#include <iostream> #include <fstream> #include <string> using namespace std; class Dog { public: Dog(){} Dog(int age,int wei) { this->m_Age = age; this->m_Wei = wei; } int m_Wei; int m_Age; }; void test() { Dog dog1(10,5); ofstream ofs; ofs.open("dog1.txt",ios::out|ios::binary); ofs<<"dog1的年龄为:"<<dog1.m_Age<<endl; ofs<<"dog1的体重为:"<<dog1.m_Wei<<endl; Dog dog2(dog1.m_Age,dog1.m_Wei); ofs.close(); ///////////// ifstream ifs; ifs.open("dog1.txt",ios::in); if(!ifs.is_open()) { cout<<"打开文件失败"<<endl; return; } ifs.read((char*)&dog2,sizeof(Dog)); cout<<dog1.m_Age<<dog1.m_Wei<<endl; ifs.close(); } int main() { test(); return 0; }
标签:22,int,Wei,Dog,ofs,2023,include From: https://www.cnblogs.com/xuxingkai/p/17421914.html