fwrite / fread question

This is a discussion on fwrite / fread question within the C Programming forums, part of the General Programming Boards category; What will happen in this situation? Code: int value; value = 0xE4F2; fwrite(&value, sizeof(char), 1, fp); What order does fwrite ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Question fwrite / fread question

    What will happen in this situation?
    Code:
    int value;
    value = 0xE4F2;
    fwrite(&value, sizeof(char), 1, fp);
    What order does fwrite use when writing bytes, and does it depend on the endianness of the machine?

    I also have the same question for fread but in reverse.

    Thanks in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > What order does fwrite use when writing bytes, and does it depend on the endianness of the machine?
    However they're stored in memory, so yes endian dependant.

    Same for fread(). So usually people pick a data format "endianness" and convert to and from if necessary on read/write. Thus it's common to pick the most common endianness your program will operate on.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    What do you think about this this function for writing numbers in big endian?
    Code:
    int bigendian_fwrite(const void* data, size_t size, size_t count, FILE* stream) {
       unsigned int i = 1;
       const char* c = (char*)&i;
       int written = 0;
       if (*c == 0)
          return fwrite(data, size, count, stream);
       else {
          c=data-1;
          while(count-- > 0){
             c+=size;
             for(i=0;i<size;i++)
                written+=fwrite(c--,1,1,stream);
          }
          return written;
       }
    }
    It works, I just think there must be a better way. Thanks for the help so far.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Can you do this with a union?

    Code:
    #include <stdio.h>
    
    union it {
    	int x;
    	char bytes[4];
    } X;
    
    void reverse () {
    	int tmp=X.bytes[0];
    	X.bytes[0]=X.bytes[3];
    	X.bytes[3]=tmp;
    	tmp=X.bytes[1];
    	X.bytes[1]=X.bytes[2];
    	X.bytes[2]=tmp;
    }
    
    int main() {
    	X.x=1000;
    	reverse();
    	printf("%d\n",X.x);		
    	reverse();
    	printf("%d\n",X.x);		
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Takteek View Post
    What do you think about this this function for writing numbers in big endian?
    Code:
    int bigendian_fwrite(const void* data, size_t size, size_t count, FILE* stream) {
       unsigned int i = 1;
       const char* c = (char*)&i;
       int written = 0;
       if (*c == 0)
          return fwrite(data, size, count, stream);
       else {
          c=data-1;
          while(count-- > 0){
             c+=size;
             for(i=0;i<size;i++)
                written+=fwrite(c--,1,1,stream);
          }
          return written;
       }
    }
    It works, I just think there must be a better way. Thanks for the help so far.
    Yes, and one thing that really helps would be to split the endian-conversion from the write. For one thing, you can do:
    Code:
       c = convert(data);
       fwrite(c, sizeof(c), 1, stream;

    Edit: Of course, your "fwrite_replacement" isn't going to work for structure or array data that isn't one contiguous data type.

    --
    Mats
    Last edited by matsp; 11-27-2008 at 03:21 AM.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another, completely different solution, is to store the data itself as text instead of as bytes. It takes about 2-3x more space for numbers, and it is a tad slower, but has the added advantage of being able to actually look at the data file with a normal text editor.

    --
    Mats
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-26-2008, 03:38 PM
  2. Question about fwrite and "swapped" numbers
    By papagaio in forum C Programming
    Replies: 7
    Last Post: 10-11-2008, 03:32 AM
  3. fwrite question
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-12-2003, 04:38 AM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM
  5. Replies: 6
    Last Post: 05-01-2003, 02:25 AM

Tags for this Thread


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