English 中文(简体)
C++ linux/mac到C++窗户的港口数据序列代码
原标题:Porting data serialization code from C++ linux/mac to C++ windows

我拥有一个软件框架,并成功地对马库和中西进行。 我现在试图把它拖到窗户(使用ming)。 迄今为止,我有软件,汇编并运行在窗户之下,但必然会合力。 尤其是,我有一个问题,即读书数据在校科(或班轮)的窗口版本中被编成序列。

序列化过程将原始变量的数值(长度、斜体、两倍等)序列化成光盘。

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));
}

因此,为了挽救一个变数组的状况,我称之为每个变量的序列号。 然后将数据重新读到,则按所节约的同一顺序重新读取。 例如:

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

改为:

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

但是,在窗口中,对序列化数据的这一读数却失败。 我猜测,窗口的序列数据没有什么不同。 我想知道,是否有办法修改上述守则,以便任何平台上所节省的数据都能在任何其他平台上阅读。

Cheers,

页: 1

最佳回答

象这种横向序列化一样,应当对这些平台进行细致的工作。 你们确实必须尊敬,但这是轻视的。 我认为这三个平台在这方面没有任何冲突。

确实,如果你做的话,你可以不作种类规格的松散。 <代码>int,float,size_tsizes can all change between programmes.

对于乱葬类型,使用鸡头的严格类型。 <代码>uint32_t,int32_t等。 Windows don t有头盔可用,但可以使用电动/电动。 h 相反。

由于大多数汇编者都遵循同样的电子电子电子计算方法,因此电离层should的工作。

C - 浮动点数(floats, Doubles)的序号:

单体序列化确实需要彻底的单位测试。 我强烈建议投入时间。

问题回答

this is just a wild guess sry I can t help you more. My idea is that the byte order is different: big endian vs little endian. So anything larger than one byte will be messed up when loaded on a machine that has the order reversed.

例如,我发现这一代典和平:

int isLittleEndian() {
    long int testInt = 0x12345678;
    char *pMem;

    pMem = (char *) testInt;
    if (pMem[0] == 0x78)
        return(1);
    else
        return(0);
}

我猜测你在气温和窗户方面将产生不同的结果。 最好的情况是,如果贵国汇编者有使用一种格式的旗帜选择。 对所有机器也是如此。

Hope this helps, Alex

Just one more wild guess: you forget open file in binary reading mode, and on windows file streams convert sequence 13,10 to 10.

• 考虑使用序列化图书馆或格式,例如:

  • XDR (supported by libc) or ASN1
  • s11n (a C++ serialization library)
  • Json, a very simple textual format with many libraries for it, e.g. JsonCpp, Jansson, Jaula, ....)
  • YAML, a more powerful textual format, with many libraries
  • or even XML, which is often used for serialization purposes...

(And for seriesization of scalars, the htonl, 并辅之以例行程序。)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签