Click here to Skip to main content
15,845,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have generated a binary file using binary writer in c# which I have to read it using CArchive class in VC++.
File contains the string of length more than 190 characters. But when I try to read it using CArchive class it reads 1 extra white space at the start of the string hence it skips the last character of the string which is causing problem in reading next integer value.
(Note: I have already applied Trim() before writing the string in file)

static void Main(string[] args)

  using (FileStream stream = new FileStream("E:\\BinaryFile.dat" , FileMode.Create))
     using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII))
          int user = 99;
          string versionMain = "Ra-1:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60];";
          versionMain += "Fast And Flurious:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60];";
          versionMain += "Pirates Of Carebian :Ra-1:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60]";
          int memDetailsKey = 1234;

In other vc++ application I am just reading this file using object of CArchive class. I am able to read first integer value but when I try to read versionMain string it reads as " Ra-1:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60];Fast And Flurious:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60];Pirates Of Carebian :Ra-1:8:10[60],9:00[60],10:01[60],11:30[60],9:10[60". Last ']' is getting skipped which is causing error in reading next integer value.

Please suggest any solution.
Updated 4-May-11 7:34am
OriginalGriff 4-May-11 5:35am    
Use the "Improve question" widget to add information: we need to see the relevant code fragments: writing to the binary file, reading from the binary file. A small (hex) sample of the data would help. Can you extract the relevant methods and try it with a small, known data sample in a separate app?

When you are using BinaryWriter the length of the string is written before the actual text. The length is written using a variable sized 7-bit encoding of the integer. Try serializing the string by first writing the lenght as a 32-bit integer, and then writing the characters using the Write(char[]) overload. This way you have better control.

Espen Harlinn
Share this answer
udayRBA 5-May-11 2:20am    
Can you please tell me how to implement this? how to write length of string as 32-bit integer?
Espen Harlinn 5-May-11 16:17pm    
for(int i = 0; i < s.Length;i++)
assuming that you can ensure that the characters are ecodable as ASCII using the ascii encoding
Sergey Alexandrovich Kryukov 9-May-11 20:59pm    
Quite important point, my 5. (I always thought one of the most stupid inventions of all times and peoples was null-terminated strings. I can literally feel your experience in Delphi and other stuff :-).
Espen Harlinn 10-May-11 3:00am    
Thank you, SAKryukov!
udayRBA 18-May-11 9:50am    
Thank you for the solution.
Actually the VC++ application in which I am reading this file is fixed and it reads the string. So I must have to write a string only. I can not write it in form of character array. Is length of the string causing any problem? Please suggest me any other solution.
This is very difficult to solve your problem for Us. you have to debug the problem at your end.

first check what exactly C# application writing into the file, i mean it is writing with space character or not.

download this Software[^] to view your binary file.

i think there is no problem in your CArchive (C++) class.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900