Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a portable way to writie a struct to a file in C?

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?

like image 204
Justin Avatar asked Nov 25 '10 05:11

Justin


People also ask

Can I write a struct to a file C?

The function to write a struct in C is fwrite(). fwrite (* struct, size, count, file); The first argument is the location of the structure to write. The second argument is the byte size of that structure.


2 Answers

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.

like image 67
Alex B Avatar answered Sep 30 '22 16:09

Alex B


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

like image 45
lijie Avatar answered Sep 30 '22 16:09

lijie