在C语言中,是否有一种跨平台的方式来存储一个变量可能包含的内容以快速重新加载它的内容?

时间:2022-05-20 12:28:40

The idea is that an application may contain a struct of large arrays that are filled up via a slow external library. So, what if that could be easily stored to a file for fast reference, at least after it has been run once? If it's not possible to be done easily in a cross platform way, is it easy to be done locally 'after a first run'?

其思想是,应用程序可能包含通过缓慢的外部库填充的大型数组结构。那么,如果可以轻松地将其存储到文件中以供快速引用,至少在它运行一次之后,该怎么办呢?如果不可能通过跨平台的方式轻松完成,那么在“第一次运行后”在本地完成是否很容易?

3 个解决方案

#1


3  

it depends of the way the structure is filled. if the structure has a fixed size (that is, it does not contain any dynamically allocated pointer) and is self-contained (it does not contain pointers to memory outside the structure itself) then you can dump the struct directly to a file using standard library file operation. something along that way:

这取决于结构的填充方式。如果这个结构有一个固定的大小(也就是说,它不包含任何动态分配的指针)并且是自包含的(它不包含在结构本身之外的内存指针),那么您可以使用标准库文件操作直接将struct转储到一个文件中。这样的话:

#include <stdio.h>

FILE *file;

file = fopen( "filename", "w" );
fwrite( &your_struct, sizeof(your_struct), 1, file )
fclose( file );

(note: error checking ommited for clarity and conciseness)

(注意:为了清晰和简洁,错误检查被省略了)

reloading looks something like this:

重新加载是这样的:

file = fopen( "filename", "r" );
fread( &your_struct, sizeof(your_struct), 1, file );
fclose( file );

this method will work on all platforms.

该方法将适用于所有平台。

however, this method is not strictly cross-platform, since the resulting file cannot be ported between machines of different endianness (for example, old Macintosh'es used to store the bytes composing an int in a different order than an IBM PC); the resulting file can only be used on platforms of the same architecture than the computer which produced the file.

但是,这种方法并不是严格意义上的跨平台,因为产生的文件不能在不同机缘的机器之间移植(例如,旧的Macintosh用于存储组成int的字节,其顺序与IBM PC不同);生成的文件只能在与生成文件的计算机相同的体系结构的平台上使用。

now if the struct is not self-contained (it contains a pointer referencing memory outside the struct) or uses dynamically allocated memory, then you will need something more elaborate...

现在,如果结构体不是自包含的(它包含一个指针在结构体外引用内存)或者使用动态分配的内存,那么您将需要更详细的东西……


regarding the endianness problem, the standard BSD socket implementation, which exists on almost every platform, defines a set of functions to convert from network byte order to host byte order (and their inverse), which are really handy, since the network byte order is strictly cross-platform. have a look at htons() and ntohs(), htonl() and ntohl(). unfortunately, you have to call those functions for each field of the structure, which is quite cumbersome if the structure is large.

关于endianness问题,几乎在每个平台上都有标准的BSD套接字实现,它定义了一组函数,可以从网络字节顺序转换为主机字节顺序(以及它们的倒数),这非常方便,因为网络字节顺序是完全跨平台的。看看htons()和ntohs()、htonl()和ntohl()。不幸的是,您必须为结构的每个字段调用这些函数,如果结构很大,这将非常麻烦。

#2


0  

Endianness is one issue, see "Writing endian-independent code in C" from IBM: http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-

在IBM的http://www.ibm.com/developerworks/aix/library/au endianc/index.html?

But you will also have to take care of "structure Padding and Alignment", see this article from C Linux Pro:

但是您还必须注意“结构填充和对齐”,请参阅C Linux Pro的这篇文章:

http : // clinuxpro.com/Cprogramming/structurepadding.php (sorry, * prevents me from using a second link, saying that this would be SPAM...)

http: // clinuxpro.com/cprogramming/ structurepadd. php(抱歉,*阻止我使用第二个链接,说这是垃圾邮件…)

Then, if you are careful about both issues, you can dump your data in a portable way.

然后,如果您对这两个问题都很小心,您可以以一种可移植的方式转储数据。

#3


0  

maybe you can store the data in XML-Format-File. With that you can avoid the problems Adrian told, and you also have no problem with language specific character codesets, and you even have the opportunity to read and write and handle the data in completly different programming languages

也许您可以将数据存储在xml格式文件中。这样,您就可以避免Adrian所讲的问题,并且您也可以使用特定语言的字符代码集,甚至还可以使用完全不同的编程语言来读写和处理数据

#1


3  

it depends of the way the structure is filled. if the structure has a fixed size (that is, it does not contain any dynamically allocated pointer) and is self-contained (it does not contain pointers to memory outside the structure itself) then you can dump the struct directly to a file using standard library file operation. something along that way:

这取决于结构的填充方式。如果这个结构有一个固定的大小(也就是说,它不包含任何动态分配的指针)并且是自包含的(它不包含在结构本身之外的内存指针),那么您可以使用标准库文件操作直接将struct转储到一个文件中。这样的话:

#include <stdio.h>

FILE *file;

file = fopen( "filename", "w" );
fwrite( &your_struct, sizeof(your_struct), 1, file )
fclose( file );

(note: error checking ommited for clarity and conciseness)

(注意:为了清晰和简洁,错误检查被省略了)

reloading looks something like this:

重新加载是这样的:

file = fopen( "filename", "r" );
fread( &your_struct, sizeof(your_struct), 1, file );
fclose( file );

this method will work on all platforms.

该方法将适用于所有平台。

however, this method is not strictly cross-platform, since the resulting file cannot be ported between machines of different endianness (for example, old Macintosh'es used to store the bytes composing an int in a different order than an IBM PC); the resulting file can only be used on platforms of the same architecture than the computer which produced the file.

但是,这种方法并不是严格意义上的跨平台,因为产生的文件不能在不同机缘的机器之间移植(例如,旧的Macintosh用于存储组成int的字节,其顺序与IBM PC不同);生成的文件只能在与生成文件的计算机相同的体系结构的平台上使用。

now if the struct is not self-contained (it contains a pointer referencing memory outside the struct) or uses dynamically allocated memory, then you will need something more elaborate...

现在,如果结构体不是自包含的(它包含一个指针在结构体外引用内存)或者使用动态分配的内存,那么您将需要更详细的东西……


regarding the endianness problem, the standard BSD socket implementation, which exists on almost every platform, defines a set of functions to convert from network byte order to host byte order (and their inverse), which are really handy, since the network byte order is strictly cross-platform. have a look at htons() and ntohs(), htonl() and ntohl(). unfortunately, you have to call those functions for each field of the structure, which is quite cumbersome if the structure is large.

关于endianness问题,几乎在每个平台上都有标准的BSD套接字实现,它定义了一组函数,可以从网络字节顺序转换为主机字节顺序(以及它们的倒数),这非常方便,因为网络字节顺序是完全跨平台的。看看htons()和ntohs()、htonl()和ntohl()。不幸的是,您必须为结构的每个字段调用这些函数,如果结构很大,这将非常麻烦。

#2


0  

Endianness is one issue, see "Writing endian-independent code in C" from IBM: http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-

在IBM的http://www.ibm.com/developerworks/aix/library/au endianc/index.html?

But you will also have to take care of "structure Padding and Alignment", see this article from C Linux Pro:

但是您还必须注意“结构填充和对齐”,请参阅C Linux Pro的这篇文章:

http : // clinuxpro.com/Cprogramming/structurepadding.php (sorry, * prevents me from using a second link, saying that this would be SPAM...)

http: // clinuxpro.com/cprogramming/ structurepadd. php(抱歉,*阻止我使用第二个链接,说这是垃圾邮件…)

Then, if you are careful about both issues, you can dump your data in a portable way.

然后,如果您对这两个问题都很小心,您可以以一种可移植的方式转储数据。

#3


0  

maybe you can store the data in XML-Format-File. With that you can avoid the problems Adrian told, and you also have no problem with language specific character codesets, and you even have the opportunity to read and write and handle the data in completly different programming languages

也许您可以将数据存储在xml格式文件中。这样,您就可以避免Adrian所讲的问题,并且您也可以使用特定语言的字符代码集,甚至还可以使用完全不同的编程语言来读写和处理数据