Binary files and cross-platform compatibility

I wrote a C ++ library that saves my data (a collection of custom structures, etc.) to a binary file. Currently, I use (that is, create and consume) files locally, on my Windows (XP) machine. For simplicity, consider the library in two parts: a writer (creates files) and a reader or consumer (just reads data from files).

Recently, I would also like to use (i.e. read) the data files that I created on my XP machine, on my Linux machine. At this point, I must point out that both machines are PCs (same as endianess, etc.).

I can build a reader (and compile for Linux [Ubuntu 9.10, to be precise]), since I am the creator of the library. My question, before I embark on this road (creating a reader, etc.):

Assuming I successfully created a reader for Linux,

Is it possible to simply copy files created on a Windows computer (XP) to a Linux machine (Ubuntu 9.10) and use the Linux reader to successfully read the copied file?

+5
source share
5 answers

For binary compatible files:

  • endianness should match (as for you)
  • The packing order of bit fields should be the same
  • size and type signature must be the same

, , , . , , / .

+14

.

, , - ints, , int. , int16, int32 ..

+2

:

  • endianess ( , )
  • , , .
  • , , , ,

, .

, "". , , . , , .

+1
+1

, .

fread fwrite, , . swap- , . , (, x86 x64). , , .

, . , .

, / . , , , ( / ).

, . , - , , , . , .

; / , fread/fwrite. (uint8_t buffer[]) , , , ( ), ( / ).

+1

All Articles