• C

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
dudubravoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.