473,466 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Writing Binary data to a file

RML
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


Jun 16 '06 #1
1 2715
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
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

Jun 19 '06 #2

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

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
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...
8
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: katz911 | last post by:
Hello, I've encountered a strange problem which I can't seem to explain. I've written a simple C++ class that deals with a certian binary file. In one of the methods there, I wish to update a...
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...
8
by: Bryan.Fodness | last post by:
Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. Here is my function that takes in...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm trying to write an array of structures named myStructArray to a binary file and later on read it back. Although I could complete the entire project in C in about 2 minutes, I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.