简体   繁体   中英

In C, is there a cross-platform way to store what a variable might contain for quick reloading of its contents?

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'?

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:

#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.

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 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-

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM