Thread: binary file again

  1. #16
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Why do you think the first few bytes indicate the length of the data?
    Because i know what this data contain (this data begin every 512 bytes with the length of the packet). but it's not so important.

    Thanks.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you run this program, you can check it's output by opening a console window, and typing:

    fc file_data copy1 <enter>

    Windows will compare the two files and print any differences, or the message:
    "no differences encountered"

    as it does here.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    long filesize(FILE *fp); 
    
    int main() {
      int n;
      long num, i;
      FILE *fp;
      char *buff;
    
      fp=fopen("file_data", "rb");  //short filename is "file_d~1"
      if(fp==NULL) {
        printf("Error opening the file");
        return 1;
      }
      printf("\n\n");
      num=filesize(fp);
      printf("Filesize of file_data is %ld bytes\n", num);
    
      buff=malloc(num);   
      if(buff==NULL) {
        printf("Error allocating memory");
        return 1;
      }
      n = fread(buff, num, 1, fp);
      if(n==0) {
        printf("Error reading the file");
        return 1;
      }
      /* just shows another way of printing out 
      the data - many char's are unprintable */
      /* 
      for(i=0;i<num;i++) {
        fputc(buff[i], stdout);
        if(i % 4 == 0 && i) 
          printf("   ");
      }  
      */
      fclose(fp);
      fp=fopen("copy1", "wb");
      if(fp==NULL) {
        printf("Error opening copy1 file");
        return 1;
      }
      fwrite(buff, num, 1, fp);
      fclose(fp);
      printf("\n\n\t\t\t     press enter when ready");
      
      i = getchar(); ++i;
      return 0;
    }
    long filesize(FILE *stream) {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    Most of the file's contents won't display, of course.

    An interesting error for me, was forgetting that sizeof(buff) stays at 2 bytes, even after it's been malloc'd to a much larger size - but that's the thing - buff hasn't been made bigger - it just has been given the address of a place in memory that is set aside for it (and it now has the address of), and that place is now bigger.
    Last edited by Adak; 09-01-2010 at 06:44 AM.

  3. #18
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by jean.yves View Post
    Because i know what this data contain (this data begin every 512 bytes with the length of the packet). but it's not so important.

    Thanks.
    OK, so I am trying to help figure out how the length of the packet is stored in the binary data using your example. So what format is the length of the packet given in? Or is the data a fixed length structure of 512 bytes?

    If the data begins every 512 bytes and thus if the longest packet size is 511 or 0x01FF, then you need at least 9 bits to give the length if all lengths are possible. However, your sample file begins with 0x07FF which is larger than 512. If it is little endian it is 0xFF07 which is still larger than 512. Your sample in message 1 begins with 0x0500 which is also larger that 512. If it is little endian, then it is 0x0005. This is less than 512.

    Do you see where I am going? I am trying to use your to samples (an very small set) to try to correctly interpret the format.

    Or are you just trying to print your array in hexadecimal format? If so, look at the (f or s) printf formats %x or %X.
    Last edited by pheininger; 09-01-2010 at 07:06 AM.

  4. #19
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks Adak for the reply, but your program display the size of the file i don't want this information, i know it!

    I will explain again my problem, suppose i have a binary file (file_data), under unix like systems i can display the contents of this file using "od -x" command for example, this is a sample of this output:


    Code:
    $od -x file_data 
    0000000      ff07    ffff    ffff    ffff    ffff    ffff    ffff    ffff
    0000020      06ff    1016    0740    fff3    ffff    ffff    ffff    ffff
    0000040      6004    0101    3074    ffff    ffff    ffff    4bff    4200
    0000060      331f    0000    0051    fff1    ffff    4bff    4200    011f
    0000100      ff04    00ff    0011    0000    ff00    1610    1939    1202
    0000120      2009    3926    0219    0912    2920    1939    1202    2009
    0000140      0000    0000    0003    0000    0000    0003    0000    0000
    0000160      0000    0000    ff01    ffff    ffff    ffff    ffff    ffff
    0000200      ffff    05ff    0505    0605    1605    4010    f307    ffff
    0000220      ffff    ffff    00ff    4132    02c4    0300    0010    0001
    0000240      0000    ffff    ffff    ffff    ffff    ffff    ffff    ffff
    0000260      ffff    ffff    0000    0000    0000    0000    0000    0000
    0000300      0000    0000    00ff    0000    ffff    ffff    ffff    ffff
    0000320      ffff    ffff    00ff    05ff    08ff    01f0    08ff    01f0
    0000340      ffff    ffff    ffff    ffff    02ff    3915    0219    0912
    0000360      ff20    5343    5049    3230    2020    ffff    ffff    ffff
    0000400      ffff    0506    5917    0078    8000    f200    ffff    ffff
    0000420      0000    00db    1412    0021    0000    60c6    4132    02c4
    I want to read this file and store this hex dump in a char buffer to do some parsing stuff. is this clear?

    Regards.

  5. #20
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    thanks pheininger for the reply,
    I want just to read the hex dump of this file and store it as it will diplayed with a command like "od -x" in a big char buffer and then i will use some pointers to do some parsing stufs.

    Regards.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So instead of displaying the data you want, pipe that output right to a file.

    You don't want to work with the binary data. You want to work with the hex representation of the data. <light bulb moment>

    Then you can use a program like the one I posted, and you'll have the data, in the buffer.
    Last edited by Adak; 09-01-2010 at 07:27 AM.

  7. #22
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Hi Adak,

    your program does not seem to do what i want it just copy the file and display his size!.

    Regards.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jean.yves View Post
    Hi Adak,

    your program does not seem to do what i want it just copy the file and display his size!.

    Regards.
    The key thing is that it takes a file, gets the size of the file, and makes the buffer exactly that size. Then it puts everything from the file, into the buffer so you can then do whatever you want to do with it.

    If you don't want to see the file size displayed, just // it out.

    Remember to open the output of the hex dump file though, NOT the original data file.

  9. #24
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Ok but the main problem is how to bufferize(store in a buffer) the data of the file as a string (hex dump) so i can manipulate them more simply.

    Example:
    suppose the hex dump of my binary file is :

    Code:
    0000000      ff07    ffff    ffff    ffff    ffff    ffff    ffff    ffff
    0000020      06ff    1016    0740    fff3    ffff    ffff    ffff    ffff
    0000040      6004    0101    3074    ffff    ffff    ffff    4bff    4200
    0000060      331f    0000    0051    fff1    ffff    4bff    4200    011f
    0000100      ff04    00ff    0011    0000    ff00    1610    1939    1202
    i want to store it in a char buffer:

    char buffer[512];

    and after running the program i can loop through my data file using this char buffer.

    Regards.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, do you know how to pipe the output of the hex dump, into a file?

    On DOS and Windows, I use >, as in:

    dir *.* >fileNames.txt

    Which puts the entire directory into a new file named fileNames.txt.

    That's using a pipe, the > char.

  11. #26
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    I don't want to use pipe or commands i receive the file from the network, and i want to parse it in a real time manner, so pipe is not a good idea(like all scripting) in a real time environnement.

    Thanks.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe this is most of what you want. Check it out:

    (still has a lot of stuff I didn't remove, but you can do that)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    long filesize(FILE *fp); 
    
    int main() {
      int j, n;
      long num, i;
      FILE *fp;
      char *buff;
      char *buff2;
      fp=fopen("file_data", "rb");  //short filename is "file_d~1"
      if(fp==NULL) {
        printf("Error opening the file");
        return 1;
      }
      printf("\n\n");
      num=filesize(fp);
      printf("Filesize of file_data is %ld bytes\n", num);
    
      buff=malloc(num); 
      buff2=malloc(num);
        
      if(buff==NULL) {
        printf("Error allocating memory");
        return 1;
      }
      n = fread(buff, num, 1, fp);
      if(n==0) {
        printf("Error reading the file");
        return 1;
      }
      printf("\n");
      for(j=0;j<num;j++) {
        if(j % 8==0) {
          printf("\n\t");
        }
        sprintf(buff2+j,"%x",buff[j]);
        printf("%x\t", buff[j]);
      }
    
      /* just shows another way of printing out 
      the data - many char's are unprintable */
      /* 
      for(i=0;i<num;i++) {
        fputc(buff[i], stdout);
        if(i % 4 == 0 && i) 
          printf("   ");
      }  
      */
      fclose(fp);
      fp=fopen("copy1", "wb");
      if(fp==NULL) {
        printf("Error opening copy1 file");
        return 1;
      }
      fwrite(buff, num, 1, fp);
      fclose(fp);
      free(buff);
      free(buff2);
    
      printf("\n\n\t\t\t     press enter when ready");
      
      i = getchar(); ++i;
      return 0;
    }
    long filesize(FILE *stream) {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    Last edited by Adak; 09-01-2010 at 09:37 AM.

  13. #28
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks Adak for the code,

    i think i need more help fix my program.... i don't understand why you want to create a copy file? i don't want this.

    Thanks again.

    Regards.

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jean.yves View Post
    Thanks Adak for the code,

    i think i need more help fix my program.... i don't understand why you want to create a copy file? i don't want this.

    Thanks again.

    Regards.
    You don't - I wanted it so I could check that the binary input and output was correct, by checking it with fc.

    You can delete that part, or save it to another file, or whatever you want.

  15. #30
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by jean.yves View Post
    thanks pheininger for the reply,
    I want just to read the hex dump of this file and store it as it will diplayed with a command like "od -x" in a big char buffer and then i will use some pointers to do some parsing stufs.

    Regards.
    What kind of parsing stuff do you want to do? I have drafted (but not tested) code which will read the binary file into a linked list of lines that are the equivalent of the output of the "od -x" command.

    If you just want it in a big character buffer, what is the biggest file you will want to process? For example, your 4KB example file when converted would be about (8*8+11/line)*256lines or about 19200 characters. Is this really what you want to manipulate?

    Are you sure you do not want to work the raw data in either bytes or words (16bit quantity) or the raw data converted to printable hex digits without whitespace. With the "od -x" format, you will have to remove (or ignore) the address part of the line or the white space between words .

    Please explain what operations you are going to do next with the data so that we could make some reasonable suggestions.

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-25-2006, 12:43 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10: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