#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    How to check what's inside a binary file


    I want to check what I've written in a binary file using fwrite.
    At first, I tried this program:
    Code:
     FILE *bip;
        int i, j;
        i = 1;
        bip = fopen("binary.bin", "wb");
        fwrite(&i, sizeof(int), 1, bip);
        fscanf(bip, "%d", &j);
        printf("j = %d\n", j);
    Contrary to my expectation, I got some garbage for j; I had expected j = 1.

    So is there a way to convert a binary file to text file, so I can actually see what I'm doing with my binary files?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    1. "wb" flag in fopen gives you only write permission. Read more about fopen.
    2. After any read or write operation on the file the position indicator is not set at the beginning of the file. You need reposition it either by using fseek or closing and opening the file again. Read more about fseek.
    3. Your fwrite call writes binary data to the file - don't use fscanf afterwards which reads text data. Use fread instead.
    4. Have you ever heard about error handling? Even in a simple test program like yours it is important to validate values returned by functions.
    Last edited by DRK82; July 19th, 2013 at 07:28 AM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    In order to see exactly what's in a binary file, you need to generate a hex dump. I always use a utility, xxd for that. But then I also work from the command line a lot.

    I wrote a web page on it: Generating a Hex Dump at http://pgm.dwise1.net/misc/hexdump.html. It includes code to add a hex dump to a program.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    @DRK82
    I addressed the issues you pointed out in the following code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define LEN 15
    
    int main()
    {
        FILE *ptr;
        int i, j, k, check;
    
        ptr  = fopen("fseek.txt", "w");
        if(ptr != NULL)
        {
            fprintf(ptr, "1 2 3 4");
            fclose(ptr);
        }
        else
        {
            printf("failure to open file\n");
        }
        fclose(ptr);
    
        ptr = fopen("fseek.txt", "r");
        if(ptr != NULL)
        {
            fscanf(ptr, "%d", &i);
            fscanf(ptr, "%d", &j);
            printf("1st digit: %d\n2nd digit: %d\n", i, j);
            check = fseek(ptr, 1, SEEK_SET);
            if(check == 0)
            {
                printf("---FSEEK---\n");
                fscanf(ptr, "%d", &k);
                printf("3rd digit: %d\n", k);
            }
            else
                printf("---FSEEK FAILED---");
        }
        else
        {
            printf("failure to open file\n");
        }
    
    
    
    }
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,889
    Rep Power
    481

    Irrelevant but useful


    The unix strings program extracts sequences of alphabet characters of minimum given length. Sometimes useful to get an idea about the content of binary files.

    It's also, great for mixed language programming. Manuals laboriously explain how compiler flags or pragmas change the markup of variable names. Extremely difficult for me to follow. Looking at readable strings from objects and libraries teaches enough in a flash.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    @dwise_aol
    I read your page, and I have a better idea of what a hex dump is, and how to interpret them.
    I checked one of my binary files in an online hex dump application here:
    http://www.fileformat.info/tool/hexdump.htm
    and got
    Code:
    0000-0006:  68 65 6c 6c-6f 00                                hello.
    I identified the three components (memory location, actual data, character interpretation), but I don't understand what the hyphen is doing there between 6c and 6f.
    Is this some kind of glitch?
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > but I don't understand what the hyphen is doing there between 6c and 6f.
    > Is this some kind of glitch?
    No, it's eye candy to make the data easier to read.

    If you try a larger file, you may see the same between bytes 8 and 9 and between bytes 12 and 13.
    In other words, it separates the data into 4-byte blocks.
    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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    If you try a larger file, you may see the same between bytes 8 and 9 and between bytes 12 and 13.
    In other words, it separates the data into 4-byte blocks.
    Oh I see, thank you.

IMN logo majestic logo threadwatch logo seochat tools logo