tags:

views:

494

answers:

2

Hello,

I have 2 applications. One in C++ (windows) open a binary file and only reads from it, i use:

fstream m_fsDataIN.open("C:\TTT", ios::in | ios::binary | ios::app);

and the second application (is in C#) opens the file and writes to it. I use:

byte[] b = ... //have a binary data
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(
                        System.IO.File.Open(@"C:\TTT",
                        System.IO.FileMode.Append,
                        System.IO.FileAccess.Write,
                        System.IO.FileShare.ReadWrite));
bw.Write(b);
bw.Flush();
bw.Close();

The problem is that the 8 first bytes are written incorrectly, comparing to what appears in the b array.

When I open the file in the C# application, using System.IO.FileMode.Append it works OK.

I checked in the application and it writes wrong 8 bytes.

I want to add that the first 8 bytes are 2 counters that each was created using IPAddressHostToNetworkAddress.

I think that the problem is in the C++ application, in how I open the file.

Help, Thnaks

A: 

You've opened it just fine. But how do you read it? I think you need to call ntohl on the C++ side.

Michael Krelin - hacker
Reading: std::vector<char> m_vctDataIN; m_vctDataIN.resize(1000); m_fsDataIN.read( int nReadBytes = m_fsDataIN.gcount(); m_nBytesInBuffer += nReadBytes; ***I found a nother thing: When The C++ application is down the C# application writes correctly. When it is up it writes wrong
Roman Dorevich
are you reading and writing to the same file simultaneously?
Michael Krelin - hacker
yes, but C# application writes and C++ reads, except in the beginning that the C# make one read and close the file.
Roman Dorevich
Well, I give up, it's either something trivial or it turns too platform-specific and the platform is not my native environment.
Michael Krelin - hacker
A: 

The problem was in the C++ application. The program contained configuration which produced another file handler. Using Process Explorer I found out about it. Removing the configuration of that extra file handler resolved the problem.

Roman Dorevich