将数据序列化代码从C ++ linux / mac移植到C ++窗口 [英] Porting data serialization code from C++ linux/mac to C++ windows

查看:147
本文介绍了将数据序列化代码从C ++ linux / mac移植到C ++窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个软件框架编译和运行成功在mac和linux。我现在试图将其移植到windows(使用mingw)。到目前为止,我有软件编译和运行在windows下,但它不可避免的bug。特别是,我有一个问题,在macos(或linux)中序列化的程序(segfaults)的Windows版本中序列化的数据。



序列化过程序列化值的原始变量(longs,ints,doubles等)到磁盘。



这是我使用的代码:

  #include< ; iostream> 
#include< fstream>

template< class T>
void serializeVariable(T var,std :: ofstream& outFile)
{
outFile.write(reinterpret_cast< char *>(& var),sizeof(var));
}

template< class T>
void readSerializedVariable(T& var,std :: ifstream& inFile)
{
inFile.read(reinterpret_cast< char *> );
}

为了保存一堆变量的状态,我调用serializeVariable变量。然后,要读取数据,调用readSerializedVariable以保存它们的顺序相同。例如保存:

  :: serializeVariable< float>(spreadx,outFile); 
:: serializeVariable< int>(objectDensity,outFile);
:: serializeVariable< int>(popSize,outFile);

并阅读:

  :: readSerializedVariable< float>(spreadx,inFile); 
:: readSerializedVariable< int>(objectDensity,inFile);
:: readSerializedVariable< int>(popSize,inFile);

但是在windows中,序列化数据的读取失败。我猜,Windows序列化数据有点不同。我想知道是否有一种方法,我可以修改上述代码,以便保存在任何平台上的数据可以在任何其他平台上读取...任何想法?



Cheers,



Ben。

解决方案

精细跨越这些平台。你必须履行字面顺序,但这是微不足道的。我不认为这三个平台在这方面有任何冲突。



但是你真的不能使用类型规范。 int float size_t



对于整数类型,请使用cstdint标头中的严格大小类型。 uint32_t int32_t 等。Windows没有iirc的标题,但可以使用boost / cstdint。



浮点应该工作,因为大多数编译器都遵循相同的IEEE规范。



C - 浮点数字的序列化(浮点数,双击)



二进制序列化真的需要彻底的单元测试。我强烈建议投资时间。


I have a software framework compiled and running successfully on both mac and linux. I am now trying to port it to windows (using mingw). So far, I have the software compiling and running under windows but its inevitably buggy. In particular, I have an issue with reading data that was serialized in macos (or linux) into the windows version of the program (segfaults).

The serialization process serializes values of primitive variables (longs, ints, doubles etc.) to disk.

This is the code I am using:

#include <iostream>
#include <fstream>

template <class T>
void serializeVariable(T var, std::ofstream &outFile)
{
    outFile.write (reinterpret_cast < char *>(&var),sizeof (var));
}

template <class T>
void readSerializedVariable(T &var, std::ifstream &inFile)
{
inFile.read (reinterpret_cast < char *>(&var),sizeof (var));
}

So to save the state of a bunch of variables, I call serializeVariable for each variable in turn. Then to read the data back in, calls are made to readSerializedVariable in the same order in which they were saved. For example to save:

::serializeVariable<float>(spreadx,outFile);
::serializeVariable<int>(objectDensity,outFile);
::serializeVariable<int>(popSize,outFile);

And to read:

::readSerializedVariable<float>(spreadx,inFile);
::readSerializedVariable<int>(objectDensity,inFile);
::readSerializedVariable<int>(popSize,inFile);

But in windows, this reading of serialized data is failing. I am guessing that windows serializes data a little differently. I wonder if there is a way in which I could modify the above code so that data saved on any platform can be read on any other platform...any ideas?

Cheers,

Ben.

解决方案

Binary serialization like this should work fine across those platforms. You do have to honor endianness, but that is trivial. I don't think these three platforms have any conflicts in this respect.

You really can't use as loose of type specifications when you do, though. int, float, size_t sizes can all change across platforms.

For integer types, use the strict sized types found in the cstdint header. uint32_t, int32_t, etc. Windows doesn't have the header available iirc, but you can use boost/cstdint.hpp instead.

Floating point should work as most compilers follow the same IEEE specs.

C - Serialization of the floating point numbers (floats, doubles)

Binary serialization really needs thorough unit testing. I would strongly recommend investing the time.

这篇关于将数据序列化代码从C ++ linux / mac移植到C ++窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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