序列化: 将数据结构或对象转换成二进制串的过程。
反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。
下面就用相关函数实现普通的字符文件操作 和 二进制文件操作。代码注释很详细
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
using namespace std;
//写文件
void WriteFile()
{
ofstream file("./text.txt",ios::out);
if (!file.is_open())
{
cout << "文件打开失败" << endl;
return;
}
file << "姓名:laymond" << endl;
file << "年龄:18" << endl;
file.close();
return;
}
//读文件
void ReadFile()
{
ifstream file("./text.txt", ios::in);
if (!file.is_open())
{
cout << "文件打开失败" << endl;
return;
}
char temp[1024] = { 0 };
//读取文件3种方式
//1、read file.eof() 作为判断条件 会慢一拍
while (file >> temp)
//while (!file.eof())
{
//file.read(temp, 1024); //这样会读取到\n
//cout << temp
// >>按行读取&