Solved

compatible files

Posted on 2002-06-10
4
142 Views
Last Modified: 2012-05-04
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
Comment
Question by:pentomino
  • 2
4 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7067658
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
 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
ID: 7067835
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
 
LVL 1

Author Comment

by:pentomino
ID: 7067876
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 7068243
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

Industry Leaders: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

713 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