Thread: Portable binary IO functions

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Portable binary IO functions

    Do they exist? So far, I only know of fread/fwrite, however the man pages has the following to say about them:
    Because of possible differences in member length and byte ordering, files written using fwrite() are application-dependent, and possibly cannot be read using fread() by a different application or by the same application on a different processor.
    So, do portable binary IO functions exist? Or is their existance fundamentally impossible?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The functions themselves are portable.
    What they write is not portable, nor can they ever be.

    If you want portable data, then you need to represent it in some other form.
    Text is one example.
    XDR is another - http://www.faqs.org/rfcs/rfc1014.html
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    What they are saying is, there is no guarantee that if you read/write data in XYZ format on some platform, you can do the same on another platform. Common example: writing entire structs to a file. The safest way is to format the data yourself in some predefined format before writing, and read the data in that format instead of relying on the system to know what to do.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks for info guys.
    My data scenario is pretty simple, I have a series of strings and integers I need to write to file. The data will be read and written by my app exclusively. I guess what I really need to know is: what is the tolerance between binary code amongst different processors/OS'es. If binary code is not interchangeable between a p3 and p4 and/or between win98 and winXP, then I may have problems, but if its more tolerable, like between an old 386 and a p4 and/or between win95 and winVista , then I won't care as much.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you're writing out structs, then even just changing the compiler options for the current compiler on the current machine can change the result (eg, packing).
    Start changing compilers and machines, and "isCompatible" becomes very fuzzy very quickly.

    Stick to text files.
    They're very portable, easy to verify, and not really that complicated to parse.
    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.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Or you format it specifically, so that say the first 2 bytes contain a number, the next 18 bytes are a string ect... then it would be cross platform compatable, that is how programs like Diablo II do their save files.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    the first 2 bytes contain a number
    And then you have to worry about endianness still.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not if you do something like this:
    Code:
    putc(x / 256, fp), putc(x % 256, fp);
    But I think it's a lot easier to just use text.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by dwks
    Not if you do something like this:
    Code:
    putc(x / 256, fp), putc(x % 256, fp);
    But I think it's a lot easier to just use text.
    That's worrying about endianness

    The person I was replying to seemed like they were implying you could just do fwrite(&some_short, sizeof(short), 1, fp)
    If you understand what you're doing, you're not learning anything.

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You can write as a bitstream so that it reads/writes Lo bit to Hi bit. That is worrying about it, but it is the way I have done it and seen it done, just write a class that does your file IO for you, that way you can read/write np.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Wraithan
    just write a class that does your file IO for you, that way you can read/write np.
    You're not exactly batting 1.000 today. There are no classes in C.
    If you understand what you're doing, you're not learning anything.

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    whoops forgot this is the C forum heh, you can still write functions to do it for you.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The portable way would be to use XML to put data to files. For simple strings and integers you could just convert all integers to strings, using fprintf() and simmilar functions to write to file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what is meant by "text file". fprintf() instead of fwrite(). 123 instead of #~.

    BTW, what XML libraries would you recommend?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks for all the suggestions guys.
    What if I were to just export characters/strings only to binary? If I were to just print them byte by byte would I avoid the binary problems associated with multi-byte reading (like with numbers)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 11-16-2006, 09:06 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Java Binary File IO
    By Wraithan in forum Tech Board
    Replies: 1
    Last Post: 07-01-2006, 04:01 AM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM