c++ 二進位制儲存檔案

心鑫發表於2014-03-30


class Stu {
	int age;
	char* address;
	std::string name;
public:
	Stu() {
	}
	Stu(int _age, char* _address, std::string _name) :
			age(_age), address(_address), name(_name) {
	}
	Stu(int _age, std::string _name) :
			age(_age), name(_name) {
	}
	void show() {
		using namespace std;
		cout << "age:" << age << ",address:" << address << ",name:" << name
				<< endl;

	}
};
int main() {
	const char* fileName = "D://test.txt";
	const char* _address = "D://test.txt";
	char _address1[80] = "D://test.txt";
	ofstream f1(fileName, ios::binary);
	Stu st1(12,const_cast<char*>(_address1), "abc");
	f1.write((char*) &st1, sizeof(st1));
	f1.close();
	ifstream f2(fileName, ios::binary);
	Stu st2;
	f2.read((char*) &st2, sizeof(st2));
	st2.show();
	return 0;

}


(char*) &st1  物件的地址轉換成char*















相關文章