Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use C++ created binary files

 
shuangquan zhang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does anyone have experience using binary files created by a C++ programe?
to be more accurate, how to read structrued data stored in a file?
like:
struct Friend{
long birthDay; // YYYYMMDD;
char name[10];
char address[40];
}
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem isn't in reading the data, you can do it with any reader object that will read the data into a byte array. The big problem is in interpretation of the byte array back into anything meaningful. C and C++ unlike java doesn't have a set size for all data types in example, Turbo-C on Win32 an integer is 16 bits whereas using gcc on Unix or Linux, it's 32 bits. If you are confident of the size of all the data types in your file, you can then convert the data into The equivalent java data type. In example, char is very easy, most compilers and platforms have it as an 8 bit, 1 byte data type which compares directly to a byte so.... (char arrarys are transfered to a byte array until a null char is found then converted to a String object). For your long, you have to know how big a long is from your C++ compiler on your platform if in example it is for sake of agruement 32 bits or 4 bytes the conversion is something like
long converted = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3];
Once you create your routine, it is only valid for systems and C, C++ comiplers that define the data types to exactly the same size .
If you are doing the coding on the C++ program yourself, you can ensure that the write method converts everything to a char array which as you can probably see provides the easiest way to ensure that the data can be converted in java programs and by other programs writen on another compiler on another platform. (Most standard dll's use this approach)
Hope this helped
 
shuangquan zhang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, Mr. Trusaik.
But what about float and double, how can I convert byte[] into an float or double?
Regards,
Sam.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!