Question about fwrite and "swapped" numbers

This is a discussion on Question about fwrite and "swapped" numbers within the C Programming forums, part of the General Programming Boards category; Hi! I'm learning about C File I/O and writing my first test programs and I'm curious about something. Here is ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Question about fwrite and "swapped" numbers

    Hi!

    I'm learning about C File I/O and writing my first test programs and I'm curious about something. Here is a function that write 2 byte signed integers to a file:

    Code:
    void writexyz(FILE* output, signed short x, signed short y, signed short z)
    {
    
    fwrite(&x, 2, 1, output);
    fwrite(&y, 2, 1, output);
    fwrite(&z, 2, 1, output);
    
    }

    And here is how it's called from my "main":

    Code:
        signed short x1, y1, z1;
        x1 = 8000;
        y1 = 0;
        z1 = -8000;
        writexyz(output_file, x1, y1, z1);
    But when I look the file, expecting to see 0x1F40, 0x0000 and 0xE0C0 the numbers are swapped: 0x1F40 becomes 0x401F and so on. Why is this? And how I can access bytes of my signed shorts so I can swap them? I know how to do this in an array but I'm not sure what's the method for an integer.

    Sorry if my questions are too basic, I'm learning C myself for fun and I'm still confused about a lot of basic things.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Note that you should use sizeof(x ... z) instead of hardcoding 2, because size is not guaranteed to be 2. Plus if you change the type, you don't need to change it there too. You're future proof, so to speak.
    In case you want to swap bytes, define a char* pointer to the address of the integer. That way, you can access the individual bytes:

    Example:
    Code:
    int x = 0x12345678;
    char* pSwap = (char*)&x;
    pSwap[0] = 0x78;
    pSwap[1] = 0x56;
    pSwap[3] = 0x34;
    pSwap[4] = 0x12;
    Last edited by Elysia; 10-10-2008 at 03:25 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Thanks, that code was instructive to me in many ways. I have to spend a few hours learning about pointers syntax, these is still something that confuses me a lot.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Elysia, can you do me a big favor and post or pm me your strtok() link as well as your analogy about houses and pointers? Gracias mi amiga.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by Elysia View Post
    Note that you should use sizeof(x ... z) instead of hardcoding 2, because size is not guaranteed to be 2.
    Bullcrap. Sizeof(short) == 2.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Bullcrap. Sizeof(short) == 2.
    Doesn't have to be according to the standard.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    china
    Posts
    16
    that's memory order
    Last edited by haochao; 10-11-2008 at 07:11 AM.

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