C++实现对象序列化和反序列化(读写二进制文件)操作

相关函数介绍

在我们的C语言中读写二进制文件一般使用的fread、fwrite全局函数,当然也可以使用更底层的read和write函数。在我们的C++中 通过ofstream 和 ifstream 对象 读写文件更加的方便了。对二进制文件的读写 主要使用 ofstream::write,ifstream::read函数。如果对文件读写方向感不强,记不住的 ,记住4个字就行了。读入写出。这个4个字是针对 程序或者说是内存!往内存里面读数据 -> read ,往磁盘里面写数据->write。这样永远就会忘了。还有一些其他的函数,都比较简单。感觉用起来很方便。

这里普及下序列化和反序列化

序列化: 将数据结构或对象转换成二进制串的过程。
反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。

下面就用相关函数实现普通的字符文件操作 和 二进制文件操作。代码注释很详细

普通文件操作

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5. //写文件
  6. void WriteFile()
  7. {
  8. ofstream file("./text.txt",ios::out);
  9. if (!file.is_open())
  10. {
  11. cout << "文件打开失败" << endl;
  12. return;
  13. }
  14. file << "姓名:laymond" << endl;
  15. file << "年龄:18" << endl;
  16. file.close();
  17. return;
  18. }
  19. //读文件
  20. void ReadFile()
  21. {
  22. ifstream file("./text.txt", ios::in);
  23. if (!file.is_open())
  24. {
  25. cout << "文件打开失败" << endl;
  26. return;
  27. }
  28. char temp[1024] = { 0 };
  29. //读取文件3种方式
  30. //1、read file.eof() 作为判断条件 会慢一拍
  31. while (file >> temp)
  32. //while (!file.eof())
  33. {
  34. //file.read(temp, 1024); //这样会读取到\n
  35. //cout << temp
  36. // >>按行读取,不会读换行符
  37. cout << temp << endl;
  38. }
  39. //2、get 一个一个字符的读取
  40. //char c;
  41. //while ( (c=file.get()) != EOF )
  42. //{
  43. // cout << c;
  44. //}
  45. //3、一行一样读取 getline 会把\n 舍弃掉....
  46. //while (file.getline(temp,1024))
  47. //{
  48. // cout << temp << endl;
  49. //}
  50. file.close();
  51. }

 

二进制文件操作(序列化和反序列化)

 

接上面代码哈,是写在同一个文件中的。

  1. class Person
  2. {
  3. public:
  4. Person(char* name, int age)
  5. {
  6. strcpy(this->name, name);
  7. this->age = age;
  8. }
  9. void showInfo()
  10. {
  11. cout << name << " " << age << endl;
  12. }
  13. public:
  14. char name[10]{ 0 };
  15. int age = 0;
  16. };
  17. //二进制文件 进行写
  18. void WriteBinaryFile()
  19. {
  20. ofstream file("./binary.txt",ios::out | ios::binary );
  21. if (!file.is_open())
  22. {
  23. cout << "文件打开失败" << endl;
  24. }
  25. Person p1("Lay1", 11);
  26. Person p2("Lay2", 2);
  27. Person p3("Lay3", 151);
  28. Person p4("Lay4", 5);
  29. Person p5("Lay5", 9);
  30. file.write((char*)&p1, sizeof(p1));
  31. file.write((char*)&p2, sizeof(p2));
  32. file.write((char*)&p3, sizeof(p3));
  33. file.write((char*)&p4, sizeof(p4));
  34. file.write((char*)&p5, sizeof(p5));
  35. file.close();
  36. }
  37. //二进制文件 进行读
  38. void ReadBinaryFile()
  39. {
  40. ifstream file("./binary.txt", ios::in | ios::binary);
  41. if (!file.is_open())
  42. {
  43. cout << "文件打开失败" << endl;
  44. }
  45. //开辟一块空间 存放读取的数据
  46. char* temp = new char[sizeof(Person)];
  47. //或者 Person p;开辟的空间肯定合适
  48. //将数据读入的 temp对应的空间
  49. while (file.read(temp,sizeof(Person)))
  50. {
  51. Person p = *(Person*)(temp);
  52. p.showInfo();
  53. }
  54. file.close();
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. //读写 字符文件
  59. //WriteFile();
  60. //ReadFile();
  61. //读写 二进制文件
  62. //WriteBinaryFile();
  63. ReadBinaryFile();
  64. return EXIT_SUCCESS;
  65. }

运行结果验证

posted on 2020-11-15 17:51  大湾  阅读(1637)  评论(0编辑  收藏  举报

导航