fopen, fwrite, and endian issues

This is a discussion on fopen, fwrite, and endian issues within the C Programming forums, part of the General Programming Boards category; Ok, I'm trying to write a file out, but the data needs to be in big endian format. Intel of ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    fopen, fwrite, and endian issues

    Ok, I'm trying to write a file out, but the data needs to be in big endian format. Intel of course is in little endian. I search around for stuff, and with the standard C library, it seems nothing is around to output easiliy from one to the other, or to even specify which endianess you are using.

    This seems like such a common issue, there is a solution and I'm not finding it. I know I can write some wrapper functions that do the byte swapping, but if it's already built in somewhere, that'd be nice to just use that. Anyone have any knowledge about this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Google is your friend.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >there is a solution and I'm not finding it
    There is a solution, but you won't like it. If you want to write a portable binary file then you'll need to use the same functions for reading it as well. These functions would perform the necessary conversions, and you would have to write them yourself or use a nonstandard library that does it for you. See the FAQ for a little more detail.

    The alternative is preferable in most cases. Simply use text files as they're far more portable.
    My best code is written with the delete key.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If he was on any os that supported sockets, he could use
    byte orders
    Host would be what the computer it was compiled on was using, and network is big endian. But that is not a solution I would recommend, listen to prelude's option.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    Google was not my friend, this time, I didn't find anything regaurding fopen and it's support of it.

    I know what endianness is, i wasn't asking that. In fact, in between the time i posted that, I already put in the swapping functions and whatnot. I was just wondering if fopen had native support.

    And for text files, rofl. text files are also a really INefficient way to store things. Not possible in this case.

    btw, I'm writing something that will import 3DS models and output a Palm Database to be read by Palm game. The Palm OS was originally written for motorola 68k, which is big endian, but not runs on ARM, for Palm OS 5+, and i'm writing an ARMlet/PNO that does all my work. I've had to deal with endian issues from the start, it's no big deal.

    For example, the matlab fopen takes a format arguement, and you can specify big endian or little endian.

    Code:
    #define ByteSwap16(n) \
    	( ((((unsigned int) n) << 8) & 0xFF00) | \
    	  ((((unsigned int) n) >> 8) & 0x00FF) )
    
    #define ByteSwap32(n) \
    	( ((((unsigned long) n) << 24) & 0xFF000000) |	\
    	  ((((unsigned long) n) <<  8) & 0x00FF0000) |	\
    	  ((((unsigned long) n) >>  8) & 0x0000FF00) |	\
    	  ((((unsigned long) n) >> 24) & 0x000000FF) )
    
    ////////////// Endian swaping wrapper functions //////////
    inline void fwrite32(UInt32 blah, FILE *f)
    {
    	UInt32 item = ByteSwap32(blah);
    	fwrite(&item, 4, 1, f);
    }
    
    inline void fwrite16(UInt16 blah, FILE *f)
    {
    	UInt16 item = ByteSwap16(blah);
    	fwrite(&item, 2, 1, f);
    }
    Last edited by FireAndGlass; 06-18-2004 at 07:33 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fopen is a standard libray function that simply opens a file. You cannot "open a file bigendian" or "open a file littleendian". There's no such thing. fopen is fopen. Nothing more, nothing less. As stated, it's up to you to make sure the data is read and write correctly.

    You can always just write a function that checks the first 4bytes of a file against a constant. Say 1 for example. Read four bytes into an integer. If it equals one, you read your file normally. Otherwise, do your magic when you read.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    unfortunately, it's practically impossible to convert arbitrary documents between the two formats - only the program that put those bytes there knows whether a given chunk is to be interpreted as a byte/word/dword/quadword/whatever. for known formats it's just a matter of cataloging as many as possible, and either skip over unknown files or else give the user more options in that event.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >text files are also a really INefficient way to store things.
    It sounds like you've been woefully misinformed. Text files aren't signifigantly less efficient in storage space used and time spent performing I/O to warrant always using binary files, and the portability advantage usually shifts the decision in favor of text files. So the efficiency argument usually doesn't hold water. I've written code to portably read and write a series of binary formats, and it's not an experience I'm keen to repeat. However, it appears that text files aren't appropriate in your case, but I only learned that on the 5th post of this thread, halfway down, where you explained what you were trying to do.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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