Bootstrap

C++——二进制文件的写入读取

二进制的文件肉眼我们是读不懂的,如果通过二进制的读写操作就可以读懂。

写操作:

#include<iostream>
#include<fstream>
using namespace std;

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

void test()
{
	ofstream ofs;
	ofs.open("Person.txt", ios::out | ios::binary);
	if (!ofs.is_open())
	{
		cout << "文件打开失败!" << endl;
		return;
	}
	Person p = { "张三", 18 };
	ofs.write((const char*)&p, sizeof(p));

	ofs.close();
}
int main()
{
	test();
	system("pause");
	return 0;
}

 

 

读操作:

#include<iostream>
#include<fstream>
using namespace std;
class Person
{
public:
	char m_Name[64];
	int m_Age;
};
int main()
{
	ifstream ifs;
	ifs.open("Person.txt", ios::in | ios::binary);
	if (!ifs.is_o
;