Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Opening an unknown binary file given its structure

Hello,

I have the structure:

struct ItemRec
{
     char   itemcode[12];
     char   indicat0;
     char   indicat1;
     char   indicat1a;
     char   filler;
     char   indicat2[2];
     char   department[4];
     char   familynumber[6];
     char   mpgroup[2];
     char   salequantity[2];
     char   saleprice[10];
     char   linkedto[4];
     char   itemname[18];
     unsigned char   userexit1[2];
     unsigned char   userexit2[2];
};

and I also have a file (a .dat binary file), and I am supposed to write a c file that reads this 5 mb file.

So I tried to create it as something like:

int main( int )
{
     struct ItemRec item;

     FILE *file = NULL;


     if ( (file = fopen("eamitemr.dat", "r")) != NULL )
     {
          while ( !feof( file ) )
          {
               int result = fread( &item, sizeof( struct ItemRec ), 1, file );
               if ( !result )
                    break;
               
               cout << item.department << endl;
               // I am choosing this field just for example! I'm kinda mixing c and cpp, hope that this isnt much of a problem
                             
          }
        fclose ( file );
     }
     return 0;
}

///////////

But no success at all. I just get junk. Am I missing some important point? What should I be looking for?

Thanks in advance for your help.
Luis
0
dudubravo
Asked:
dudubravo
  • 6
  • 4
  • 2
1 Solution
 
jkrCommented:
>>Am I missing some important point?

If you want to read a binary file, open it in binary mode:

   if ( (file = fopen("eamitemr.dat", "rb")) != NULL )

Disregard this comment if you are not working on a MS system :o)


0
 
dudubravoAuthor Commented:
jkr,

I did that too and it did output the same junk.

Funny thing about it is that if I "edit with notepad" this .dat file, I see the first lines of the binary code describing the file name and other stuffs, then afterward I see the rest of the products and etc.

Would it then be required to have a hold of the program that wrote this file and analyze the code. I mean, would this be the only way?

Thanks,
Luis
0
 
jkrCommented:
Um, one other thing - you should change the structure packing for ItemRec. What compiler are you using?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
honey_hamsterCommented:
If you want to read the file and know what you're reading, you have to know its exact format, e.g. are the char[] arrays NULL-terminated, are there any extra spaces?  The best way to do this is to look at the source code of the program that wrote the file.  It's also helpful to see the hex values of the data in the file.  Editing with Notepad will only show the data that corresponds to printable ASCII characters.
0
 
dudubravoAuthor Commented:
jkr,

If it is not too much pain, I am working at my school now and I published the files I am working on at:

cissbt.gsw.edu/help

There is an explanation file there .doc you can read if you need
0
 
dudubravoAuthor Commented:
honey master,

I hadn't thought of that (hex). I probably need to know how to interpret it though.
0
 
jkrCommented:
Hm, again, what compiler are you using?
0
 
dudubravoAuthor Commented:
MS Visual C++
0
 
jkrCommented:
Ahh, great - try


#pragma pack ( 1) // turn of packing alignment
struct ItemRec
{
    char   itemcode[12];
    char   indicat0;
    char   indicat1;
    char   indicat1a;
    char   filler;
    char   indicat2[2];
    char   department[4];
    char   familynumber[6];
    char   mpgroup[2];
    char   salequantity[2];
    char   saleprice[10];
    char   linkedto[4];
    char   itemname[18];
    unsigned char   userexit1[2];
    unsigned char   userexit2[2];
};

This is pretty much the reason for the junk, as the compiler aligns every struct member on a DWORD boundary by default.
0
 
dudubravoAuthor Commented:
jkr,

i copied and pasted it and still no success... :( (still getting junk)
0
 
honey_hamsterCommented:
The "typedef struct tagItemRec" that's in the .h file on the website (and the one you posted in your original question) doesn't match the file format as described in 4690EAMITEMR.doc, e.g. ITEMCODE is described in the document as being 6 bytes long, but there are 12 bytes allocated in the structure.  There are other mismatches similar to this as well.  One safe way to read this file (and not worry about how the compiler packs the structure), is to explicitly read each field individually, e.g. read 6 bytes from the file and assign this to the itemcode field, then read a byte and assign it to the indicat0 field, etc.  4690EAMITEMR also says that there's a variable length USERDATA field at the end of each record, but in my quick read of the of the document, I didn't see how you can figure out how long this USERDATA field is for each record.

Good luck.
0
 
dudubravoAuthor Commented:
honey hamster,

you are the man! i learned how to read the hex code and used it against the .dat file to get my structure right.

thanks bunch,
luis
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now