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.