Advertisement

No way to write a file in VB and read it in C?

Started by November 17, 2000 02:43 PM
5 comments, last by vbisme 24 years, 1 month ago
How can I write a file in Visual Basic and read it in C/C++. What about the other way around?
Sure you can do that. What exactly are you having problems with?
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
The problem is that it won''t read the correct information. I saved an integer in VB and read it in C/C++ but it won''t read the correct value.
Wanna post some code?

Another thing. How large is an integer in VB? is it 16 bits or 32 bits? I''m asking this because VB does have a long datatype and I''m not sure whether the integer has become 32 bit under VB4 and later. If it isn''t that''s probably your problem because an integer under a 32 bit C compiler is going to be 4 bytes large.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
I would suggest getting one of the hex editors from somewhere like CNet or Ziff-Davis. Then write what you think is the same file in both languages. Then use the hex editor to compare the two. You could also try opening the VB one in notepad since the problem may be that it is text and not binary. Since the problem right now is an integer you could try writing one with both and compare the lengths of the files. If one is a length of 2 and the other 4 then you know the problem is differant sized integers. Overall a hex editor is going to do the most to help you with conversion problems.
Keys to success: Ability, ambition and opportunity.
This part in Visual Basic 6:

Type GFXHeader
NumberOfFiles As Integer
End Type
---------------------

Dim FileNum As Integer
Dim myHeader As GFXHeader

FileNum = FreeFile
Open "myFile.fil" For Binary As FileNum
myHeader.NumberOfFiles = 10
Put #FileNum, , myHeader
Close FileNum

This will save the file, and could be reopen and read by VB. But in C/C++:

#include "iostream.h"
#include "windows.h"

class GFXHeader
{
public:
int NumberOfFiles;
};

int main()
{
HANDLE myFile;
DWORD NumBytesRead;
GFXHeader myHeader;

myFile = CreateFile("myFile.fil", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

ReadFile(myFile, &myHeader.NumberOfFiles, sizeof(GFXHeader), &NumBytesRead, NULL);

CloseHandle(myFile);

return(0);
};

The read out for myHeader.NumberOfFiles is an uninitialized number like -89353985 something.

Edited by - vbisme on November 18, 2000 12:49:04 PM
Advertisement
and vb integer: Dim myInteger As Integer range from -32768 to 32767, which is 2^16 = 65536...16 bits bingo...Thanks

Edited by - vbisme on November 18, 2000 1:09:50 PM

This topic is closed to new replies.

Advertisement