14

I need to serialize a C struct to a file in a portable way, so that I can read the file on other machines and can be guaranteed that I will get the same thing that I put in.

The file format doesn't matter as long as it is reasonably compact (writing out the in-memory representation of a struct would be ideal if it wasn't for the portability issues.)

Is there a clean way to easily achieve this?

Miguel
  • 1,966
  • 2
  • 18
  • 32
Justin
  • 84,773
  • 49
  • 224
  • 367

3 Answers3

18

You are essentially designing a binary network protocol, so you may want to use an existing library (like Google's protocol buffers). If you still want to design your own, you can achieve reasonable portability of writing raw structs by doing this:

  1. Pack your structs (GCC's __attribute__((packed)), MSVC's #pragma pack). This is compiler-specific.
  2. Make sure your integer endianness is correct (htons, htonl). This is architecture-specific.
  3. Do not use pointers for strings (use character buffers).
  4. Use C99 exact integer sizes (uint32_t etc).
  5. Ensure that the code only compiles where CHAR_BIT is 8, which is the most common, or otherwise handles transformation of character strings to a stream of 8-bit octets. There are some environments where CHAR_BIT != 8, but they tend to be special-purpose hardware.

With this you can be reasonably sure you will get the same result on the other end as long as you are using the same struct definition. I am not sure about floating point numbers representation, however, but I usually avoid sending those.

Another thing unrelated to portability you may want to address is backwards compatibility by introducing length as a first field, and/or using version tag.

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • It supports C++, not C, but here is a link to Google's protocol buffers page: http://code.google.com/apis/protocolbuffers/ – Alex Reynolds Nov 25 '10 at 06:22
  • Thanks - having done some more research based on this answer I also found the following articles on portable C code which might be useful to anyone else who finds this question: http://serghei.net/docs/programming/autobook-1.1/writing20portable20c.html and http://www.chris-lott.org/resources/cstyle/portableC.html – Justin Nov 25 '10 at 06:34
  • @Alex Reynolds Really? This one seems quite active. http://code.google.com/p/protobuf-c/ – Alex B Nov 25 '10 at 12:06
  • @Justin Both your links are now 404 :-( – Duncan Jones Aug 07 '13 at 13:02
  • 2
    @DuncanJones Wayback machine to the rescue! [First](http://web.archive.org/web/20110918055802/http://serghei.net/docs/programming/autobook-1.1/writing20portable20c.html), [Second](http://web.archive.org/web/20110725164734/http://www.chris-lott.org/resources/cstyle/portableC.html) – Justin Aug 07 '13 at 13:15
2

You could try using a library such as protocol buffers; rolling your own is probably not worth the effort.

lijie
  • 4,811
  • 22
  • 26
0

Write one function for output. Use sprintf to print an ascii representation of each field to the file, one field per line.

Write one function for input. Use fgets to load each line from the file. Use scanf to convert to binary, directly into the field in your structure.

If you plan on doing this with a lot of different structures, consider adding a header to each file, which identifies what kind of structure it represents.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141