二进制文件读写发现奇怪问题
FileStream fs = new FileStream(path, System.IO.FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("11111");
写完后用UE打开发现第一个字节是0x05,后面的正常,在写其他东西发现都一样,第一个字节是写入的长度,请问大侠谁知道怎么回事啊?还有,有没好的办法写入VC++ 6.0里面的结构体?
比如我定义的
char name[20]
char desc[100]
其实name一般也就10个byte以内,但是协议定的就20个字节,如果用bw还要计算实际的长度,然后补0进去,好麻烦
FileStream fs = new FileStream(path, System.IO.FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("11111");
写完后用UE打开发现第一个字节是0x05,后面的正常,在写其他东西发现都一样,第一个字节是写入的长度,请问大侠谁知道怎么回事啊?还有,有没好的办法写入VC++ 6.0里面的结构体?
比如我定义的
char name[20]
char desc[100]
其实name一般也就10个byte以内,但是协议定的就20个字节,如果用bw还要计算实际的长度,然后补0进去,好麻烦
解决方案 »
- 用户登陆首页的密码验证问题
- C# Directory.Exists()判断访问局域网共享文件夹,如果不能访问,则等待时间很长,求其它解决办法?
- C#将table直接粘贴到excel中
- 知道今天的日期,怎样得到15天后的日期?
- 关于WIN7下面 notifyicon 无法变换。
- 急,如何才能将MySQL数据库中的汉字和TEXT字段读出来?(200分)
- setup project做出的install能够显示License Box和修改注册表吗? 在线等待,立刻给分。
- 当方法中有out 或者ref参数时,应该怎么用MethodInfo.Invoke(...)调用
- 控件的问题
- 怎样能使一个窗体永远悬浮在另一个窗体之上
- 大家在用vs2008的时候有没有遇到过vs自己死掉重启
- //我这里定义一个Dictionary,并添加数据 Dictionary<string,string[]> dicData=new Dictionary<str
string path,
byte[] bytes
)
byte[] array,
int offset,
int count
)
像
Write(Boolean)
Write(Byte)
Write(Double) 等,
因此需要写入长度
想再请问下C++中struct
{
char Num[20];
char Code[64];
int index;
}
这个结构体写入文件,C#该怎么写啊?
byte[] array,
int offset,
int count
)
byte[] array,
int offset,
int count
)
用[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]写结构体,然后
序列化
public static byte[] rawSerialize(object obj)
{
int rawsize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(obj, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
}
最后System.IO.FileStream.Write写
而是
public virtual void Write(
string value
)
string类型的overload是特殊的参考msdn
A length-prefixed string represents the string length by prefixing to the string a single byte or word that contains the length of that string. This method first writes the length of the string as a four-byte unsigned integer, and then writes that many characters to the stream. This method writes a length-prefixed string to this stream using the BinaryWriter instance's current Encoding