2012-01-12 138 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

回答

相關問題