• 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;


  • 相关阅读:
    WCF三种通信方式
    Linux发布WebApi
    Supervisor Linux程序进程管理
    Centos安装Mongodb
    本地网址连不上远程mysql问题
    .Net之垃圾回收算法
    .Net之托管堆资源分配
    Centos7+ASP.Net Core 运行
    ASP .Net Core 使用 Dapper 轻型ORM框架
    转载 Jquery中AJAX参数详细介绍
  • 原文地址:https://www.cnblogs.com/cy163/p/737147.html
Copyright © 2020-2023  润新知