Reading Writing Binary data to a file

G

Guest

Hi,

I have a MFC C++ application which write the data in a structure out to a
file. Here is the structure...

typdef struct {
short ID;
TCHAR Num[10];
short x;
} TestStruct;

I then have a VB.NET app which reads the file data in using a BinaryReader.
Here is the structure I save it to.

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure PartBlock_Struct
Public ID As Short ' Part Identifier. See
PARTS_STRUCT above
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)> _
Public Num As String ' Circuit Test Number.
Public x As Short
End Structure

When I read in the data (VB.NET app) all is find. My problem is when I
write it back out in the VB.NET app. I use a BinaryWriter to do this. Here
is the code.

bw.Write(PartBlock.ID)
bw.Write(PartBlock.Num)
bw.Write(PartBlock.x)

When the 1st bw.Write executes, the BinaryWritter's Position is 2, thats
good. After the next bw.Writer runs, the Position is 13! I would expect it
to be 12. Because of this, the "x" value is offset by 1 byte, hence when I
read this file back in, "x" value is incorrect.

Can anyone help?

RML
 
G

GhostInAK

Hello RML,

Have you popped the file open in a hex editor to see what the extraneaous
byte is? That might give you a clue or two.

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Reading a binary file 2
Reading Binary file created by VC++ 1
Converting Block of Bytes to Structure 2
VB structures and Marshalling 12
Setting based 1 Arrays 113
HELP Please!!!! 3
DLL Callback 2
Threads and Callbacks 19

Top