Writing to binary

Started by
3 comments, last by Programmer One 19 years, 6 months ago
Hi... I was wondering, if im outputing an integer to a file opened for binary output with vb6, will I be able to read the integer with a c++ program that opens a file for binary input? I know for types like strings, vb is way to high-level, but for bytes,integers and singles(float) , would that be ok? Thanks.
Advertisement
Quote:Original post by md_lasalle
Hi... I was wondering, if im outputing an integer to a file opened for binary output with vb6, will I be able to read the integer with a c++ program that opens a file for binary input?

I know for types like strings, vb is way to high-level, but for bytes,integers and singles(float) , would that be ok?

Thanks.


In theory it should work but in practice it might not, check the data types and their sizes and compare between C++ and VB. I think some of the data types have different sizes from their C++ counterparts (ints, shorts and longs i believe). If you read and write the same sized (in bytes) information then it will work fine.

HTH
nice, exactly the type of answer i was looking for... confirming what i thought. :)

Byte -> unsigned char (8 bits)
Integer -> signed short (16 bits)
Long -> signed int (32 bits)
Single -> float (32 bits)
Double -> double (64 bits)

Format, endianess, etc are identical.

As for strings... have a VB app write a single string to a blank file, and then crack it open in a hex editor and decode the format, and write a string-loading wrapper for your C/++ app. Not hard at all.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
If you want added power in VB, you could always import functions from the Windows API, such as WriteFile().

Here is an example:

What you would import:
Private Declare Function GetStdHandle Lib "kernel32" ( _    ByVal nStdHandle As Long) As LongPrivate Declare Function ReadFile Lib "kernel32" ( _    ByVal hFile As Long, lpBuffer As Any, _    ByVal nNumberOfBytesToRead As Long, _    lpNumberOfBytesRead As Long, _    lpOverlapped As Any) As LongPrivate Declare Function WriteFile Lib "kernel32" ( _    ByVal hFile As Long, lpBuffer As Any, _    ByVal nNumberOfBytesToWrite As Long, _    lpNumberOfBytesWritten As Long, _    lpOverlapped As Any) As Long


Write:
Function StdWrite(ByVal Text As String) As Long  Const STD_OUTPUT_HANDLE = -11&  Dim Handle As Long    Handle = GetStdHandle(STD_OUTPUT_HANDLE)  WriteFile Handle, ByVal Text, Len(Text), StdWrite, ByVal 0&End Function


Read:
Function StdRead() As String  Const STD_INPUT_HANDLE = -10&  Dim Handle As Long  Dim Buffer As String * 2048  Dim Bytes As Long    Handle = GetStdHandle(STD_INPUT_HANDLE)  Do    ReadFile Handle, ByVal Buffer, Len(Buffer), Bytes, ByVal 0&    If Bytes < Len(Buffer) Then Exit Do    StdRead = StdRead & Buffer  Loop  StdRead = StdRead & Left$(Buffer, Bytes)End Function


Of course you can impliment that in whatever way you need to, but just know that you have the full power of the Win32 API, even in Visual Basic.

This topic is closed to new replies.

Advertisement