二进制读文件 //文件输入流对象 可以通过read函数,以二进制方式读取数据

#include
#include
#include
using namespace std;

//二进制文件 读文件

class Person
{
public:
char m_Name[64]; //姓名
int m_Age; //年龄
};

void test01()
{
//1.包含头文件

//2.创建流对象
fstream s;

//3.打开文件 判断文件是否
s.open("person.txt", ios::in | ios::binary);

if (!s.is_open())
{
	cout << "打开失败" << endl;
	return;
}

//4.读写文件
Person p;

s.read((char*)&p, sizeof(Person));  //二进制读文件

cout << "姓名:" << p.m_Name << endl;
cout << "年龄:" << p.m_Age << endl;


//5.退出文件

s.close();

}
int main()
{
test01();
system(“pause”);
return 0;
}

//文件输入流对象 可以通过read函数,以二进制方式读取数据

猜你喜欢

转载自blog.csdn.net/ADADQDQQ/article/details/108300377