c++ 用文件保存类对象

 
#include  
#include  
#include  
using namespace std; 

int main() 
{ 
	
	ofstream file; 
        file.open("winIp.txt",ios::binary); 
       //缓存的类型是 unsigned char *,需要类型转换
       file.write((char *)&winServer,sizeof(winServer));  //winServer为类对象
       file.close(); 
	
	
	ifstream Myfile2; 
	file2.open("winIp.txt",ios::binary);  
        //二进制打开,缺省为文本,ios::out,ios::in,文本输入输出用<<,>>    
	file2.read((char *)&winServer,sizeof(winServer)); 
	int num=Myfile2.gcount(); //获取读到文件的大小
        file2.close();

	return 0; 

}


你可能感兴趣的:(c++ 用文件保存类对象)