473,471 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Binary file created by VC++

RML
Hi,

My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++
WinCE app. The VC++ app writes the following structure to the file.

typedef struct {
short ID;
TCHAR Num[10];
} PARTS_STRUCT;

In my VB code I read the file with this code...

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure Parts_Struct
Public ID As Short
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10)> _
Public Num As String
End Structure

Dim Parts As New Parts_Struct
Dim fs As New FileStream(FName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Parts.ID = br.ReadInt16()
Parts.Num = br.ReadChars(10)

In the binary file, the Num fiedl contains the text "123".

When I run the code, the ID field is Ok, but I only get the 1st letter if
the "Num" field ("0")..

If I change the Num field type to Char(), it gets filled with this...
Num[0] = "1"
Num[1] = Nothing
Num[2] = "2"
Num[3] = Nothing
Num[4] = "3"
Num[5] = Nothing

I also tried changing the "CharSet" parameter in the Structlayout() to Ansi
and Unicode, bit still get the same results.

Looks like I am close, but still missing something.
Can anyone point me in the right direction?

RML

May 10 '06 #1
1 1491
RML wrote:
Hi,

My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++
WinCE app. The VC++ app writes the following structure to the file.

typedef struct {
short ID;
TCHAR Num[10];
} PARTS_STRUCT;

In my VB code I read the file with this code...

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure Parts_Struct
Public ID As Short
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10)> _
Public Num As String
End Structure

Dim Parts As New Parts_Struct
Dim fs As New FileStream(FName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Parts.ID = br.ReadInt16()
Parts.Num = br.ReadChars(10)

The file is binary - you can't read characters, you have to read bytes
and then convert them into a string.

T
May 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
2
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: RML | last post by:
Hi, My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++ WinCE app. The VC++ app writes the following structure to the file. typedef struct { short ID; TCHAR Num; }...
1
by: Mr X | last post by:
One of my employer's products, written in VB6, reads and writes binary files. We are shortly going to port to VB.NET (framework version 2.0) and will need to be able to read and write these binary...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.