?
Solved

Opening an unknown binary file given its structure

Posted on 2003-02-28
12
Medium Priority
?
532 Views
Last Modified: 2012-05-04
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
Comment
Question by:dudubravo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8044182
>>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
 

Author Comment

by:dudubravo
ID: 8044582
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
 
LVL 86

Expert Comment

by:jkr
ID: 8044622
Um, one other thing - you should change the structure packing for ItemRec. What compiler are you using?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:honey_hamster
ID: 8044759
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
 

Author Comment

by:dudubravo
ID: 8044800
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
 

Author Comment

by:dudubravo
ID: 8044813
honey master,

I hadn't thought of that (hex). I probably need to know how to interpret it though.
0
 
LVL 86

Expert Comment

by:jkr
ID: 8044829
Hm, again, what compiler are you using?
0
 

Author Comment

by:dudubravo
ID: 8044845
MS Visual C++
0
 
LVL 86

Expert Comment

by:jkr
ID: 8044880
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
 

Author Comment

by:dudubravo
ID: 8044895
jkr,

i copied and pasted it and still no success... :( (still getting junk)
0
 
LVL 2

Accepted Solution

by:
honey_hamster earned 300 total points
ID: 8045133
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
 

Author Comment

by:dudubravo
ID: 8062296
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question