2012-01-12 119 views
0

我想读取VB6中创建的二进制文件,反之亦然。VB6和VC++二进制文件读取/写入

是否有任何数据类型转换我不必担心从C++转到VB6,反之亦然?

C++中用于VB6布尔型数据类型的等效类型是什么?

这里是我的C++结构:

struct FooBarFileC 
{ 
    long int foo; 
    int bar; 
}; 

这是我在VB6类型:

Public Type FooBarFileVB 
    foo As Long 
    bar As Integer  
End Type 

我在VB6读二进制文件代码:

Dim fooBarvb As FooBarFileVB 
Dim strOptionsFileName As String 
strOptionsFileName = "someFile.bin" 

If Dir(strOptionsFileName) <> "" Then 
    file_length = FileLen(strOptionsFileName) 
Else 
    file_length = 0 
End If 

fileNumber = FreeFile 

If (file_length <> 0) Then 
    Open strOptionsFileName For Binary Access Read Lock Read Write As #fileNumber 
    Get #fileNumber, , fooBarvb 
    Close #fileNumber 
End If 

foo = foobarvb.foo 
bar = foobarvb.bar 

我用C++读取二进制文件的代码:

long int foo; 
int bar; 
FooBarFileC cFooBar; 

ifstream fin("someFile.bin", ios::binary); 
fin.read((char*)&cFooBar, sizeof(cFooBar)); 
fin.close(); 

foo = cFooBar.foo; 
bar = cFooBar.bar; 

我在VB6

foobarvb.foo = foo 
foobarvb.bar = bar 

If Dir(strOptionsFileName) <> "" Then 
    file_length = FileLen(strOptionsFileName) 
Else 
    file_length = 0 
End If 

fileNumber = FreeFile 

If (file_length <> 0) Then 
    Open strOptionsFileName For Binary Access Write Lock Read Write As #fileNumber 
    Put #fileNumber, , fooBarvb 
    Close #fileNumber 
End If 

我的代码编写的二进制文件在C++

long int foo; 
int bar; 
FooBarFileC cFooBar; 

cFooBar.foo = foo; 
cFooBar.bar = bar; 

ofstream fout("someFile.bin", ios::binary); 
fout.write((char*)&cFooBar,sizeof(cFooBar)); 
+0

你的VB6的声明是错误的,INT =长。 – 2012-01-12 19:57:00

+0

它可能只是一个复制和粘贴错误,但您的VB写入方法正在打开文件以进行读取。另外,除非您真的在意文件存在并且已经有内容,否则打开文件“For Binary”将会创建该文件,如果该文件尚不存在的话。 – jac 2012-01-12 20:06:14

+0

谢谢,这是一个复制粘贴错误。 @Hans - 那么另一个也应该很长?那么C++中的long int怎么样,VB6中的正确类型是什么? – NexAddo 2012-01-12 20:40:20

回答