binary file again

This is a discussion on binary file again within the C Programming forums, part of the General Programming Boards category; Hi all, I need to read a binary file as a string and store it into a buffer to do ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    63

    binary file again

    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:
    #include <stdio.h>
    #include <stdlib.h>
    
    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't use fgets() for binary data. Use fread() (and fwrite() if needed).

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks for the reply, but if i use fread i got the same output:
    fread(buffer, sizeof(buffer), 1, in).

    Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand your fread() statement:
    Code:
    //or in this case, your fread():
    fread(buffer, sizeof(buffer), 1, in).
    That would try to read in the whole buffer size, with every read. I doubt that is what you want. Try 5, instead.

    fread(buffer, sizeOfTheItemYouWantToRead, Number of ItemsYouWantPerRead, in)

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks Adak,

    With your fread, i have a bus error and the same output as before!
    My file is 4k and i want to split it to 8 buffers of size 512 Bytes each to do some parsing. that's why i'm trying to read my whole buffer size.

    Ragards.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
       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);         
       }
    When you copy over 5 elements of buffer to head_string, you have NOT made a string. Add an end of string char: '\0', to the end of the head_string. So you need at least 6 elements in the head_string array, to hold 5 elements, and one end of string marker char.

    Bayint Naung has a good point: If you are working with binary data, it may be better to use fwrite() to stdout, to display what you want. stdout is a file stream that is automatically opened for output to the screen, whenever your C program starts.
    Last edited by Adak; 09-01-2010 at 04:36 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Agrr Have you tried to use debugger yet?
    printf() for binary input is not going to work. %s needs C style string.(null terminated char array).
    Your data may have null byte also.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks all,

    your answers will not change anything! i think that the problem come from the formatting of readed data in the binary file.

    Ragrds.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up your binary file on Swoopshare and give us a link to it, and I GUARANTEE our answers will change what's happening.

    We just can't give you exactly the answers you want/need, without having the file.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    adak, write to stdin?

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    you can download the binary file here:

    file_data - 4shared.com - online file sharing and storage - download

    Thanks.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Bayint Naung View Post
    adak, write to stdin?
    Ha ha - that was a TEST to see if you were awake!!

    I am barely awake and will edit that to avoid confusion - thanks.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    140
    Quote Originally Posted by jean.yves View Post
    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.

    ...

    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.
    Quote Originally Posted by jean.yves View Post
    you can download the binary file here:

    file_data - 4shared.com - online file sharing and storage - download

    Thanks.

    The file you provided for download does not match the display you show in message #1.

    Neither file starts with a printable character string (with or without a null terminating character).

    The download file is 4096 (0x1000) bytes long but starts with 0x07 0xFF 0xFF 0xFF which seems to have no correlation to the length. Of course, I cannot tell how large the file is in message #1.

    Why do you think the file starts with a printable string indicating its length?
    Last edited by pheininger; 09-01-2010 at 06:30 AM. Reason: Removed the code which was an imcomplete edit.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    in message 1 it was just a sample! what this will really change in the final solution? it's just a binary data.

    Regards.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    140
    Quote Originally Posted by jean.yves View Post
    in message 1 it was just a sample! what this will really change in the final solution? it's just a binary data.

    Regards.
    Why do you think the first few bytes indicate the length of the data?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2006, 11:43 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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