mon*_*ing 0 c++ binary struct fwrite fread
我可以假设使用fwrite生成的文件和使用fread读取的文件可以跨不同系统移植.32位/ 64位Windows,osx,linux.
//dumping
FILE *of =fopen("dumped.bin","w");
double *var=new double[10];
fwrite(var, sizeof(double), 10,FILE);
//reading
file *fo=fopen()
double *var=new double[10];
fread(var,sizeof(double),10,of);
Run Code Online (Sandbox Code Playgroud)
结构怎么样?
struct mat_t{
size_t x;
size_t y;
double **matrix;
}
Run Code Online (Sandbox Code Playgroud)
这些便携式吗?
简答:没有
答案很长:
您正在写出数据的二进制表示.
这不能跨平台或操作系统甚至编译器移植.
你写的所有对象都有可以改变的东西:
int: size and endianess.
double: size and representation.
structure: Each member has to be looked at individually.
The structure itself may be padded different on different compilers.
Or even the same compiler with different flags.
pointers: Are meaningless even across processes on the same machine.
All pointers have to be converted into something meaningful like
a named object that is provided separately. The transport will then
have to convert named objects into pointers at the transport layer
at the destination.
Run Code Online (Sandbox Code Playgroud)
您有两个主要选择: