binary file again

Discussion in 'C' started by jean.yves, Aug 31, 2010.

  1. jean.yves

    jean.yves New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    I need to read a binary file as a string and store it into a buffer to do some stuff, but when i run my program i got some strange characters.

    Here is my program:
    Code:
    int
    main (int argc, char **argv)
    {
       FILE                *in;
       char                head_string[5];
       int                 j;
       char                buffer[512];
    
       in = fopen(argv[1], "rb");
    
       if(in == NULL) {
           fprintf (stderr, "Can't open file.\n");
           return EXIT_FAILURE;
       }
    
       while (fgets(buffer, sizeof(buffer), in) != NULL) {
           for (j = 0; j < 5 ; j++)
                   head_string[j] = buffer[j];
           printf("The length of the block is : %s\n", head_string);         
       }          
    
       fclose(in);
    
       return 0;
    }
    When i run my program (passing a binary file as a parameter) i got a strange chars:

    Code:
    $ ./a.out log_dump.data
    The length of the header is : )
    The length of the header is : ????
    The length of the header is : S%a0
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : ?????
    The length of the header is : )
    The length of the header is : ????
    The length of the header is : SF0i
    
    the display of my file using an hex dump:


    Code:
    0500    0033    5100    f100    ffff    00ff    0000    0000
    0000    0000    0000    0000    0000    0000    0001    ffff
    ffff    ffff    ffff    ffff    ffff    00ff    00ff    0500
    f008    ff01    f008    ff01    0002    4233    0219    0912
    ff20    5343    5049    3130    2020    ffff    ffff    ffff
    ffff    0506    2926    0007    fff0    ffff    ffff    ffff
    0000    4900    0101    2122    0000    4900    328a    ca41
    0002    0033    5100    f100    ffff    ffff    0000    4200
    2117    0100    1400    53f2    6125    0030    3921    33f0
    9371    7372    fff2    ffff    00ff    4207    2117    0100
    1400    53f8    4606    6930    9477    06f0    9371    9372
    Thanks for your help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice