Solved

compatible files

Posted on 2002-06-10
4
138 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now