zoukankan      html  css  js  c++  java
  • 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;


  • 相关阅读:
    Python深入:编码问题总结
    Search for a Range
    Search in Rotated Sorted Array
    WebStrom 多项目展示及vuejs插件安装
    高强度减脂Tabata练习
    webStrom 美化
    myeclipse 与 webstrom 免解析node_modules 的方法
    node-webkit 入门
    vue框架搭建
    Electron_01
  • 原文地址:https://www.cnblogs.com/cy163/p/737147.html
Copyright © 2011-2022 走看看