当前位置 博文首页 > 文章内容

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

    作者: 栏目:未分类 时间:2020-11-15 18:00:34

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    相关函数介绍

    在我们的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. }

    运行结果验证