Is there a cross-platform way in C to store what a variable can contain to quickly reload its contents?

The idea is that the application may contain a structure of large arrays that are populated through a slow external library. So what if it can be easily saved in a file for quick reference, at least after it has been run once? If this cannot be done on a cross-platform path, is it easy to do it locally "after the first launch"?

+4
source share
3 answers

depends on how the structure is filled. if the structure has a fixed size (that is, it does not contain a dynamically assigned pointer) and is autonomous (it does not contain pointers to memory outside the structure itself), then you can send the structure directly to a file using the standard library file. something like that:

#include <stdio.h> FILE *file; file = fopen( "filename", "w" ); fwrite( &your_struct, sizeof(your_struct), 1, file ) fclose( file ); 

(note: error checking is obvious for clarity and conciseness)

the reboot 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 transferred between machines of varying degrees (for example, old Macintosh used to store bytes that make up int in a different order than the IBM PC); the resulting file can only be used on platforms of the same architecture as the computer that created the file.

now if the structure is not autonomous (it contains a pointer that refers to memory outside the structure) or uses dynamically allocated memory, then you will need something more complex ...


regarding the endianness problem, the standard implementation of the BSD socket, which exists on almost every platform, defines a set of functions for converting from a network byte to a host byte order (and their reverse), which are really convenient since the network byte order is strictly cross-platform. look at htons() and ntohs() , htonl() and ntohl() . unfortunately, you must name these functions for each field of the structure, which is rather cumbersome if the structure is large.

+3
source

Endianness is one of the problems, see "Writing Entity-Independent Code in C" from IBM: http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs -

But you also have to take care of “Laying and Aligning the Structure”, see this article from C Linux Pro:

http://clinuxpro.com/Cprogramming/structurepadding.php (sorry, Stackoverflow does not allow me to use the second link, saying that it will be SPAM ...)

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

0
source

Perhaps you can store the data in XML-Format-File format. With this, you can avoid the problems that Adrian said, and you also have no problems with language character codes, and you even have the ability to read and write and process data in completely different programming languages.

0
source

All Articles