程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 探析C#文件方式讀寫結構體(2)

探析C#文件方式讀寫結構體(2)

編輯:關於C語言

i1 和 i2 這兩個 int 字段共享與 lg 相同的內存位置。使用平台調用時,這種結構布局控制很有用。

我做了一個簡單的測試程序,基本達成預定需求,不過程序該方式要求比較苛刻,如果要解析的數據與轉換的結構體不匹配就會引發一系列莫名其妙的異常(如內存不可讀等等之類),下面是測試程序的源代碼,有興趣的朋友可以看一看,也希望網友能提出更好的方案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System..Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace RWFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//從文件中讀結構體
private void button1_Click(object sender, EventArgs e)
{
string strFile = Application.StartupPath + "\\test.dat";
if (!File.Exists(strFile))
{
MessageBox.Show("文件不存在");
return;
}
FileStream fs = new FileStream(strFile, FileMode.Open,
FileAccess.ReadWrite);
TestStruct ts = new TestStruct();
byte[] bytData = new byte[Marshal.SizeOf(ts)];
fs.Read(bytData, 0, bytData.Length);
fs.Close();
ts = rawDeserialize(bytData);
textBox1.Text = ts.dTest.ToString();
textBox2.Text = ts.uTest.ToString();
textBox3.Text = Encoding.Default.GetString(ts.bTest);
}
//向文件中寫結構體
private void button2_Click(object sender, EventArgs e)
{
string strFile = Application.StartupPath + "\\test.dat";
FileStream fs = new FileStream(strFile, FileMode.Create ,
                      FileAccess.Write);
TestStruct ts = new TestStruct();
ts.dTest = double.Parse(textBox1.Text);
ts.uTest = UInt16.Parse(textBox2.Text);
ts.bTest = Encoding.Default.GetBytes(textBox3.Text);
byte[] bytData = rawSerialize(ts);
fs.Write(bytData, 0, bytData.Length);
fs.Close();
}
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)] //,Size=16
public struct TestStruct
{
[MarshalAs(UnmanagedType.R8)] //,FIEldOffset(0)] 
public double dTest;
[MarshalAs(UnmanagedType.U2)] //, FIEldOffset(8)]
public UInt16 uTest;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
                 //, FIEldOffset(10)]
public byte[] bTest;
}
//序列化
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;
}
//反序列化
public static TestStruct rawDeserialize(byte[] rawdatas)
{
Type anytype = typeof(TestStruct);
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length) return new TestStruct();
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(rawdatas, 0, buffer, rawsize);
object retobj = Marshal.PtrToStructure(buffer, anytype);
Marshal.FreeHGlobal(buffer);
return (TestStruct)retobj;
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved