0

I'm trying to write 4 unsigned short int numbers represented in hex into a binary file.

I keep getting a lot of junk in addition to the numbers I'm writing, and I'm not sure why. I'm assuming that in the translation from unsigned short int to binary, there's junk being written into my file.

My function to write into my binary file is:

int write_file  (char* name, unsigned short int array[x] ){

    FILE *fptr;
    fptr = fopen(name, "wb");
    if(fptr==NULL)
    {
        fclose(fptr);
        return (0);
    }
    unsigned short int code = 0xABCD;
    const void *ptr = &code;
    fwrite(ptr, sizeof(ptr), 1, fptr);

    unsigned short int code2 = 0x0000;
    const void *ptr2 = &code2;
    fwrite(ptr2, sizeof(ptr2), 1, fptr);

    unsigned short int code3 = 0x0001;
    const void *ptr3 = &code3;
    fwrite(ptr3, sizeof(ptr3), 1, fptr);

    unsigned short int code4 = 0x1101;
    const void *ptr4 = &code4;
    fwrite(ptr4, sizeof(ptr4), 1, fptr);

    return (0);
}

Ideally the binary file would be interpreted as:

ABCD 0000 0001 1101

But I'm getting this instead:

abcd f250 0190 0000 0000 eeb6 1915 7ffd
0001 eea6 1915 7ffd 1101 ee96 1915 7ffd
3
  • Please note that unsigned short int is guaranteed to be 16bit length minimum; but it is implementation dependence. Please use byte precision to create your binary file; Commented Apr 15, 2020 at 0:37
  • 1
    Or, just use a data type that is guaranteed to be 16 bits regardless of implementation, like uint16_t in <stdint.h> Commented Apr 15, 2020 at 0:38
  • Also, mind the endianness of your binary data! (When I was an undergrad, my school did have both big-endian and little-endian computers, but that might be less of an issue today.) Commented Apr 15, 2020 at 5:05

1 Answer 1

5

You are passing the wrong byte sizes to fwrite().

You are passing the byte size of each void* variable. sizeof(void*) is 4 bytes in a 32bit build, and 8 bytes in a 64bit build (sizeof(void*) would appear to be returning 8 in your case). You need to instead pass the byte size of the unsigned short int variables (2 bytes) that are being pointed at.

You don't need the void* variables at all. And don't forget error checking:

int write_obj_file  (char* filename, unsigned short int program_bin[ROWS] ){

    FILE *fptr = fopen(filename, "wb");
    if (fptr == NULL)
        return -1;

    unsigned short int code = 0xABCD;
    if (fwrite(&code, sizeof(code), 1, fptr) < 1) {
        fclose(fptr);
        return -1;
    }

    unsigned short int code2 = 0x0000;
    if (fwrite(&code2, sizeof(code2), 1, fptr) {
        fclose(fptr);
        return -1;
    }

    unsigned short int code3 = 0x0001;
    if (fwrite(&code3, sizeof(code3), 1, fptr) {
        fclose(fptr);
        return -1;
    }

    unsigned short int code4 = 0x1101;
    if (fwrite(&code4, sizeof(code4), 1, fptr) {
        fclose(fptr);
        return -1;
    }

    fclose(fptr);
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

You are quick my friend, very, very quick :)
Really quick; fflush is missing though! :-)
@choppe fclose() will flush the file if it hasn't been flushed yet.
if fptr is actually NULL, do I need to run fclose on it?
@GordWait no. If fopen() returns NULL, there is nothing to close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.