Thread: working with binary files

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    working with binary files

    I have a school homework to read the partition table from a binary file mbr.bin
    root@marlboro ~/fdisk # file mbr.bin
    mbr.bin: x86 boot sector, code offset 0x48
    http://cs.pub.ro/~programare/Teme_de_casa/tema6/mbr.bin

    how can i do that? any ideea can help!

    i've tryed

    char a[512];
    f=fopen("mbr.bin","rb");
    fread(a,1,512,f);
    but i don't know how to interpret the data from the char array a.
    Please pretty please help!

  2. #2
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    You mean to tell us that they didn't give you any pointers on interpreting the data in the mbr.bin file?

    Or did you just not pay attention?

    RTFM
    {RTFM, KISS}

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    they only gave us the same aproach you gave me... so please try to have a more constructive advice.
    If you have a sugestion is really apreciated!
    Thank you very much!

  4. #4
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    Quote Originally Posted by spank
    If you have a sugestion is really apreciated!
    Quote Originally Posted by spoon_
    RTFM
    http://www.google.com
    {RTFM, KISS}

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    i've searched one hour on google and nothing really usefull came up... any ideeas ?
    Thanks!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    i've read some docs. now i don't understand why the first byte from mbr.bin is displayed "eb" in a hex editor and if i read it in c

    fread(a,1,512,f);
    x=a[0];
    printf("%x ",x);

    it shows me "ffffffeb". This happens not only with the first byte. But there are some that are displayed the same like in the hex editor
    Help!

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use unsigned char rather than char. A plain char may be signed, as appears to be the case on your system.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    i`ve found a python code:
    part_types = {
    0x00: 'Empty',
    0x01: 'DOS 12-bit FAT',
    .....
    0x50: 'DM (disk manager)',
    0x51: 'DM6 Aux1 (or Novell)',
    ...

    i was wondering if i can do smthing like that on c.
    if i say part_type[0x51] to go to DM6 Aux1 (or Novell)
    Last edited by spank; 12-26-2005 at 06:41 AM.

  10. #10
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    You could (for example) use an array of strings and access the strings via array indices (just assign the unused indices an empty string). And do yourself a favour and read a nice tutorial. You'll find plenty.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    yes i've solved that... now how can i read the bites from a byte.
    for ex i have a char and i want to separate all his bits

    thank you!

  12. #12
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    You should read the chapter about "bitwise operators" in your favourite tutorial/book. Especially bitwise AND (&) and bitshift (<< >>) will be important.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Code:
    void byte2bit(unsigned char a)
    {
     char s[8];
     int ibit;
     s[0] = '\0';
         for (ibit = 0; ibit < 8; ibit++)
         {
             if ((a >> ibit) & 1)
             {
                            strcat (s, "1");
             }
             else
             {
                            strcat (s, "0");
             }
     }
     
     puts(s);
    }
    do you have other ideeas?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Replace 8 with CHAR_BIT if you want to be accurate. Oh and you have that whole problem of not having enough room in your string for the null character. This means either:

    1 - You're running off the end of your array, with strcat.
    2 - You somehow escape trashing your buffer, and instead have no null, so puts vomits the contents of your memory to your screen, assuming that doesn't crash, until it hits a byte that's equivalent to a null, at which point it stops.
    3 - You somehow luck out, and it appears to work for a while, however, since you've already done something bad, the rest of us just wait for your next question of "why does my program crash at <some entirely seemingly unrelated point in your program>?"


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Binary Files Problem
    By terran9 in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2004, 10:23 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  4. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 10:12 PM
  5. Opening files in binary mode
    By ammar in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2002, 04:14 PM