Go Premium for a chance to win a PS4. Enter to Win

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

compatible files

I need to have visual basic 6.0 write and read a binary file that can also be read be a c program.
Can anyone provide me with a simple example?
Need to write strings, a character,floating point number, and a structure.

Thanks
0
pentomino
Asked:
pentomino
  • 2
1 Solution
 
gencrossCommented:
Here is an example of reading/writing a binary file.  I have not looked at the code, but it may be helpful.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=14583&lngWId=1
0
 
rspahitzCommented:
VB can write "binary" files by simply encoding the pieces any way you want.  Then C can read those by unencoding from the same process.

Are you looking for a specific predefined format, like packed decimal or "zip" format?

To write data from VB:

strXXX = "testing"
chrX="@"
dblXXX=3.14159
open "somefile.bin" for output as #1
print #1, strXXX; chrX; dblXXX;
close #1

' resulting file:
somefile.bin
------------
testing@3.14159

Obviously, the extraction will not work well unless you know the length of the string and maybe the type of data, so you would probably want to add something like this:

print #1, chr$(len(strXXX));strXXX;

On the C-end, it would need to understand the style using a mapping mechanism.  It may just be simpler to create an ActiveX control to do all the work and add methods for "CreateFile", "AddItem", "GetItem", etc.
0
 
pentominoAuthor Commented:
rspahitz

Well, I was hoping I would not have to tweak the C code at all.  If I have to modify how it reads the data, I just may go with a text file.

Pentomino
0
 
rspahitzCommented:
Well, then, find out the format that C is using and write VB code to put it in that format.  I haven't checked out gencross' link, btu maybe that will handle it or get you close.

The only caveat I can see is that the data format may change depending on the O/S.  For example, a 32-bit O/S may write a number as 4 bytes, but a 64-bit O/S may write the same number with 4 additional bytes with all zeros.  This would cause some incompatibility and require some sort of upgrade tool for "old" files being moved to the new O/S.
0

Featured Post

Technology Partners: 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!

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