를 포함하는 구조체를 읽고 난 다음 구조체가 : 나는 성공적 이진 파일이 작성했습니다문자 배열
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() 메서드를 시도했지만 나에게 도움이되지 못했습니다. 이 구조체를 읽으면서 도와주세요.
수동으로 일련 화하는 이유가 있습니까? – LightStriker
"작동하지 않음"을 정의하십시오 - 무슨 일이 있었습니까? –
@ Marc-AndréJutras 특별한 이유가 없습니다. 하나는 BinaryFormatter.Serialize를 사용하여 직렬화 할 수도 있지만 클래스에만 해당됩니다. 나는 구조체를 사용하고있다 ... –