Fixed Length Binary I/O

This is a discussion on Fixed Length Binary I/O within the C Programming forums, part of the General Programming Boards category; I have 2 integer in my structure Code: struct{ int a; int b; } i hv to write a fixed ...

  1. #1
    Registered User
    Join Date
    May 2007

    Fixed Length Binary I/O

    I have 2 integer in my structure
       int a;
       int b;
    i hv to write a fixed length binary format in a text file. but what i dont understand is how to do this particular problem. For example, if the value of a is equal to 48, it must be stored as 48 (in decimal)
    or 30 (in hexadecimal; in C: 0x30). It must not be stored as the string 48,
    occupying two bytes.


  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    ...binary format in a text file...
    These two things are typically mutually exclusive.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    use fopen with "wb" mode to open file as binary
    and fwrite to write a struct as binary
    To be or not to be == true

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    But, given that structure, if you're expecting say 00 48 to appear in the file as two bytes, then you need to do something yourself.

    Writing the whole structure in one step, whilst possible, it not portable. There are padding, alignment and endian issues which may get in the way of the result you expect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree and File I/O
    By Matus in forum C Programming
    Replies: 24
    Last Post: 12-05-2008, 11:41 AM
  2. Need help with a snake game (ncurses)
    By Adam4444 in forum C Programming
    Replies: 11
    Last Post: 01-17-2007, 02:41 PM
  3. File I/O Binary Question...
    By C+noob in forum C++ Programming
    Replies: 11
    Last Post: 07-11-2005, 07:51 AM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21