在C Sharp中使用Binary Writer编写字符串. [英] String writing using Binary Writer in C Sharp.

查看:67
本文介绍了在C Sharp中使用Binary Writer编写字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用c#中的二进制编写器生成了一个二进制文件,我必须使用VC ++中的CArchive类来读取它.
文件包含长度超过190个字符的字符串.但是当我尝试使用CArchive类读取它时,它在字符串的开头读取了1个额外的空格,因此它跳过了字符串的最后一个字符,这在读取下一个整数值时会引起问题.
(注意:我已经在文件中写入字符串之前应用了Trim())

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;
          writer.Write(user);
          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]";
          writer.Write(versionMain.Trim());
          int memDetailsKey = 1234;
          writer.Write(memDetailsKey);
     }
  }
}




在其他vc ++应用程序中,我只是使用CArchive类的对象读取此文件.我能够读取第一个整数值,但是当我尝试读取versionMain字符串时,它读为"Ra-1:8:10 [60],9:00 [60],10:01 [60],11:30 [60 ],9:10 [60];速度与激情:8:10 [60],9:00 [60],10:01 [60],11:30 [60],9:10 [60];海盗Carebian:Ra-1:8:10 [60],9:00 [60],10:01 [60],11:30 [60],9:10 [60.最后一个]"被跳过,这导致读取下一个整数值时出错.

请提出任何解决方案.




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.

推荐答案

使用BinaryWriter时,字符串的长度写在实际文本之前.使用整数的可变大小的7位编码来写入长度.尝试通过首先将长度写为32位整数,然后使用Write(char [])重载来写字符来序列化字符串.这样您可以更好地控制.

问候
Espen Harlinn
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.

Regards
Espen Harlinn


这很难为我们解决您的问题.您必须在最后调试问题.

首先检查C#应用程序到底要写入什么文件,我的意思是它是否使用空格字符进行写入.

下载此软件 [
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.


这篇关于在C Sharp中使用Binary Writer编写字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆