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. }

運行結果驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM