我有以下结构:
unsafe struct Locomotive
{
public fixed char locotype[6];
public int roadno,HP;
}
我已成功将其写入二进制文件。这是代码:
Locomotive l1 = new Locomotive();
for (int i = 0; i <= 5; i++)
{
l1.locotype[i] = textBox1.Text[i];
}
l1.roadno = int.Parse(textBox2.Text);
l1.HP = int.Parse(textBox3.Text);
BinaryWriter bw = new BinaryWriter(File.Open(@"C:\Documents and Settings\Ruchir Sharma\Desktop\Locodata.bin", FileMode.Append));
IntPtr ip = Marshal.AllocHGlobal(Marshal.SizeOf(l1));
Marshal.StructureToPtr(l1, ip, true);
Byte[] b1 = new Byte[Marshal.SizeOf(l1)];
Marshal.Copy(ip, b1, 0, b1.Length - 1);
bw.Write(b1);
MessageBox.Show("Data written successfully");
Marshal.FreeHGlobal(ip);
bw.Close();
现在,在阅读这个结构时,字符数组,即 locotype[6] 给了我一个问题。我尝试了 BinaryReader.ReadChars() 方法,但它对我不起作用。请帮助我阅读此结构。