Thread: Writing binary data to a file

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007

    Writing binary data to a file


    I'm reading and writing data to a file like:
    int c = 10;
    /* ... */
    fp = fopen("file.dat", "wb");
    fwrite(&c, sizeof(c), 1, fp);
    /* read by later ... (not by the same program that made it necessarily) */
    How can I ensure that other architectures (and OSes) can read it? ie 32 or 64bit versions of my program? Should I be writing each byte in a certain order? Or maybe some define, like int32_t ?


  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    The best way would be to write a write/read pair of functions. Pick a format that you want the file to be in. You could write it all out in either endian format, as long as your reading and writing is expecting the same file format.

    Your current call to fwrite() isn't what you would achieve obviously. You need to write it byte by byte unfortunately. You could buffer it yourself in a char array, actually, if you think it necessary.

    Edit: Another way to do it is to take a guess on the architecture you'll be using most of the time and adjust the file format to be friendly to that. For example, if you're planning on your program being used on a little endian 32-bit system, then your file format can be just that. That can let you do direct calls to fread() and fwrite() if your program determines that the program is being run on a system with that architecture, and otherwise it can read and write the long way should it find itself on a system that does not satisfy those requirements.

    There's probably a really easy solution to this, but I'm probably totally missing it.
    Last edited by MacGyver; 10-23-2007 at 10:19 PM.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    hmm thanks MacGyver

    > There's probably a really easy solution to this, but I'm probably totally missing it.
    That's why I asked, maybe there is an easier way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    There is really no EASY way to do this.

    Since the program itself is the only part that knows what the format of a struct or datatype is, it will need to understand the way that the data is stored to disk.

    There are two "obvious" solutions:
    1. Whenever a file needs to be portable, use a text or "standard" binary format [using little endian is probably a good idea, since a vast majority of systems use little endian nowadays - although there are stilll several processors using big endian].

    2. Create a function that takes a struct of type x, and converts it into a binary array from it, store that - and of course a binary array to struct. [this is similar to the "standard binary format"]. It would be possible to write a parser for struct types to allow this process to be automated.

    If you use C++, you could make all members have a "store/load me" function.

    The only really easy way is to store files as text - that also has the extra advantage that the content can be read in a text-editor [which of course is also a drawback].

    One thing I would definitely recommend is that you use your own defined types, such as UINT64, UINT32, UINT16, UINT8, SINT32, etc. That way, you can have one place where you check if a 64-bit int is "long" or "long long".

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Hmm, thanks -- I've decided to go with text files, since speed isn't really an issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Writing to a Binary File
    By Lah in forum C Programming
    Replies: 1
    Last Post: 10-27-2003, 01:41 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM