Thread: Binary File I/O

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Binary File I/O

    I'm making an adventure game IDE that will output a directory containing a platform-independent C/C++ source code file and all the necessary graphics files and other resources. Instead of taking up all that storage on both the hard disk and in RAM while the project is being developed, I designed the engine to be VERY modular and I'm able to store all the necessary project data in a very compact struct. When the user wants to save their project but not "compile" it into the ready-to-compile source code and graphics form, I was going to use binary I/O to write the struct to the file. When loading a project, all I would have to do would be to read in the struct. The problem is, I want this program to have as few OS limitations as possible, and while I will have to make several different versions of the IDE for all OSs, it would be nice if the project files could be saved on one system and then reopened on another, but in a book I was reading it said that binary file i/o tended to cause problems on cross-platform programs. Would this single-struct file format cause any problems? Would it be best just to write individual variables in ASCII format?

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    if you are careful, you can write out integers and individual fields from each struct in a specific byte ordering and avoid such platform concerns

    it would likely be easier to write ascii routines, though. i recommend using "s-expressions" to write out your file data (check google)
    .sect signature

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Byte ordering.... I remember reading something about that... thanks a lot. If it works that's exactly what I want. I know MS systems "reverse" memory. Like if I wanted to write û to a file (ASCII character 150) it would write 01101001 instead of the 10010110 you'd expect by converting 150 to binary. Does this happen in Macs and unix-based systems?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    idea: if there is a problem like that, I can write a function that reverses the nibbles before writing or after reading so it's always Windows-Standard, and use that function in what ever IDEs need it.

  5. #5
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    bit ordering is not important, so long as you limit all file read/write operations to work on a byte-by-byte basis. byte ordering is not determined by the operating system, but by the host computer - if you want absolute portability, you must use the same byte ordering in your file format for all systems, but have code which changes the byte ordering to the host machine when reading.

    another problem is that many compilers pad structs with extra bytes to improve memory read speed. so, if you write out an entire struct, you will also write these extra padding bytes - and different compilers on different platforms may choose different padding sizes or not at all. so, you cannot conveniently use 'fwrite' to write out data if you want portability between different processors, much less compilers

    argh, to clarify - you'll need to read and write individual bytes and use the shift operator to put them in their proper places

    it might be possible to use network byte-ordering functions to do this for you, but i am not certain. somebody else?
    Last edited by ggs; 08-20-2004 at 10:49 AM.
    .sect signature

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Can that last problem also be solved by carefully arranging the order in which struct members are written?

    edit: And to do that Il'l just write struct members, not the whole struct.

  7. #7
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    some compilers allow this:

    #pragma pack(1)

    or some similar directive to turn off packing, but i do not believe that is standard between all compilers
    .sect signature

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I was going to use binary I/O to write the struct to the file.
    >Would it be best just to write individual variables in ASCII format?

    http://www.eskimo.com/~scs/C-faq/q20.5.html
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Binary FIle I/O
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 03-21-2003, 02:22 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. I/O to binary file
    By pors7 in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2001, 02:27 PM