Quote:[1]Posted by TheShotGunnner on 24 Nov 2002 09:16 PM[/1]
Is it possible to read a C++ written binary file into visual basic and write it again using vb then have it read by c++. If it is then can someone please tell me. also how do you write a typefef structure into a file using c++. Does anyone know. I would really appreciate it.
A binary file is a binary file regardless of which language creates it. The following code reads in a binary file 2 bytes at a time (the read statement) and then writes them to another binary file 2 bytes at a time (the write statement)
#include <fstream.h>
#include <iomanip.h>
int main()
{
int ind, tot;
unsigned char byte[2];
ifstream infile("2000_09_28_18_40_11_Sen2_Grp0.dat", ios::in );
ofstream outfile("test.dat", ios::binary );
for ( ind = 1; ind <= 20; ind++ )
{
infile.read( byte, 2 );
outfile.write( byte, 2 );
tot = byte[0] + ( byte[1] << 8 );
cout << setw(8) << ind << setw(8) << tot << setw(8) << (int)byte[0] << setw(8) << (int)byte[1] << endl;
}
return 0;
}
I plan to test test.dat later and make sure that VB can read it. But I have little doubt that it will. You may wonder what I was writing to the screen with cout. (This is important to consider.) Often binary files contain 2 byte integers split into two separate bytes. Here I was taking the bytes and recombining them into their original value. For example the first two number that are read are 208(byte[0] ) and 7 (byte[1]). Shifting the the 1 byte 7 8 places (the equivalent of multiplying by 256) and then adding it to byte[0] to make an integer 2 bytes long equals 2000. 7 * 256 + 208 = 1792 + 208 = 2000 or looked at another way 00000111 010110000 is a 16 bit representation of 2000.
I hope this helps.
David