• An equivelent to CFile::Write(); Object serilization




    I have a struct

    struct S{
    string Str;
    int Score;
    }



    to store personal info. In VC++ 6.0, I can use CFile::Write() to those struct instances, corresponding to individual personal info, into hard disk file.

    However, in a pure C++ framework, how to do this,
     tha answer is Object Serialization The following is the example code from a post by Mark in www.codeproject.com

    // Another simple example - Your class should probably have a constructor!  #include <string>#include <iostream>using namespace std; class S{   
      string Str;   
      int Value;
      public:   
        friend ostream& operator<< (ostream& os, S& s);   
          friend istream& operator>> (istream& is, S& s);
    }; 
    ostream& operator<< (ostream& os, S& s){   
    os << s.Str;   os.put('\n');   
    os.write((char *)&s.Value, sizeof(int));   
    return os;} 
    istream& operator>> (istream& is, S& s){  
     is >> s.Str;   
    is.get();   
    is.read((char *)&s.Value, sizeof(int));   
    return is;
    }
     
    // Write an S object   
     S s;   
    ofstream myfile("c:\\testmyfile.ext" , ios::binary : ios::trunc);   
    myfile << s;
    // Read an S object   
     S s;   
    ifstream myfile("c:\\testmyfile.ext" , ios::binary);   
    myfile >> s;


  • 相关阅读:
    SIFT,SURF,ORB,FAST,BRISK 特征提取算法比较
    OpenCV 4.2.0 编译成功日志(Mac)
    Ceres Solver Bibliography
    Linux下重启就需要重新激活eth0的解决办法(ifup eth0)
    PS(光影魔术手)
    软件项目开发各阶段文档模板(参考)
    敏捷、瀑布开发模式
    QA
    QC
    会计人必知的实务基础知识
  • 原文地址:https://www.cnblogs.com/cy163/p/737147.html
Copyright © 2020-2023  润新知