将c ++类转换为字节数组 [英] converting c++ class to a byte array

查看:280
本文介绍了将c ++类转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过使用ios :: binary

$ b将下面的Student类转换为二进制文件

  #include < fstream> 
#include< iostream>
using namespace std;

class Student {
public:
char name [40],address [120],gender;
double age;
bool is_blabla;
};

int main(){

学生一;
strcpy(one.name,Cancan Can);
strcpy(one.address,example example exampla);
one.gender ='M';
one.age = 25;
one.is_blabla = true;

ofstream ofs(fifthgrade.ros,ios :: binary);
ofs.write((char *)& one,sizeof(one));

学生二;
ifstream ifs(fifthgrade.ros,ios :: binary);
ifs.read((char *)& two,sizeof(two));

//检查数据是否正确
cout<< 学生姓名:< two.name<< endl;


return 0;
}

该文件如下所示:



http://i.imgur.com/GfkheWs.png p>

那么在这种情况下,我怎么做同样的事情来转换类为一个字节(或说char *)数组?



编辑:但在这种情况下,让我们假设我有一个类有方法。你给出的答案告诉我编写我自己的位运算符来序列化一个复杂的类。你可以参考一个好的源代码教它如何做或一个小样本/示例吗?



Edit2:我必须避免使用额外的库,因为我可能反序列化在机器/编译器上的序列化代码,我无法导入大多数库(例如,我将尝试反序列化在nvcc编译代码上的代码)。



示例dummy类将是这样:

  class Student {
public:
char name [40] [120]性别;
double age;
bool is_blabla;

void set_values(int,int);
int doubleage(){return age * 2;}
};


解决方案

只需使用 std: :stringstream 而不是 std :: ofstream



c $ c> char * 使用 std :: stringstream :: str()(这个返回的是一个 std :: string 使用 std :: string :: c_str()检索 char * c>)。


It is possible to convert the following Student class to a binary file by writing it with ios::binary

#include <fstream>
#include <iostream>
using namespace std;

class Student{
    public:
        char name[40], address[120], gender;
        double age;
        bool is_blabla;
};

int main() {

    Student one;
    strcpy(one.name, "Cancan Can");
    strcpy(one.address, "example example exampla");
    one.gender = 'M';
    one.age = 25;
    one.is_blabla = true;

    ofstream ofs("fifthgrade.ros", ios::binary);
    ofs.write((char *)&one, sizeof(one));

    Student two;
    ifstream ifs("fifthgrade.ros", ios::binary);
    ifs.read((char *)&two, sizeof(two));

    // check if the data is OK
    cout << "Student Name: " << two.name << endl;


    return 0;
}

The file looks like as follows:

http://i.imgur.com/GfkheWs.png

So how can I do the same thing to convert the class to a byte (or say char*) array in this case?

Edit: But in this case, Let's say I have a class having methods. The answers you gave is telling me to write my own bitwise operators to serialize a complex class. Can you refer to a good source teaching how it can be done or a small sample/example for it?

Edit2: I have to avoid using extra libraries because I may deserialize the serialized code on a machine/compiler that I cannot import most of the libraries (for example I will try to deserialize the code on an nvcc compiled code).

Example dummy class would be like this:

class Student{
    public:
        char name[40], address[120], gender;
        double age;
        bool is_blabla;

        void set_values (int,int);
        int doubleage() {return age*2;}
};

解决方案

Just use a std::stringstream rather than std::ofstream.

Then, access the char* using std::stringstream::str() (This return's a std::string, on which you can retrieve a char* using std::string::c_str()).

这篇关于将c ++类转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆