C#中将结构类型数据存储到二进制文件中方法
以往在vb6,vc6中都有现成的方法将结构类型数据写入和读取到二进制文件中,但是在c#中却没有现成的方法来实现,因此我查阅了一些资料,借鉴了网上一些同学的做法,自己写了个类似的例子来读写结构类型数据到二进制文件中,废话不多说了,先上代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filename = @"d:\testbinary.st";
#region 结构体
[StructLayout(LayoutKind.Sequential), Serializable]
public struct MY_STRUCT
{
public double x; //点的经度坐标
public double y; //点的纬度坐标
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
public string Name; //Name[40]; //名称
public long PointID; //点的ID号
public long TypeCode; //客户不使用该字段
}
#endregion
public void WriteInfo(byte[] bt)
{
if (File.Exists(filename))
{
File.Delete(filename);
return;
}
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bt);
bw.Flush();
bw.Close();
fs.Close();
MessageBox.Show("保存成功!");
}
public byte[] ReadInfo(string file)
{
FileStream fs = new FileStream(file, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bt = br.ReadBytes(144);
br.Close();
fs.Close();
return bt;
}
private MY_STRUCT Byte2Struct(byte[] arr)
{
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
IntPtr ptemp = Marshal.AllocHGlobal(structSize);
Marshal.Copy(arr, 0, ptemp, structSize);
MY_STRUCT rs = (MY_STRUCT)Marshal.PtrToStructure(ptemp, typeof(MY_STRUCT));
Marshal.FreeHGlobal(ptemp);
return rs;
}
private byte[] Struct2Byte(MY_STRUCT s)
{
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
byte[] buffer = new byte[structSize];
//分配结构体大小的内存空间
IntPtr structPtr = Marshal.AllocHGlobal(structSize);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(s, structPtr, false);
//从内存空间拷到byte数组
Marshal.Copy(structPtr, buffer, 0, structSize);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return buffer;
}
private void button1_Click(object sender, EventArgs e)
{
MY_STRUCT[] arr = new MY_STRUCT[2];
MY_STRUCT np = new MY_STRUCT();
np.x = 112.123456;
np.y = 21.56789;
np.Name = "深圳市政府1";
np.PointID = Convert.ToInt64(1234);
np.TypeCode = Convert.ToInt64(65);
arr[0] = np;
np = new MY_STRUCT();
np.x = 113.123456;
np.y = 22.56789;
np.Name = "深圳市政府2";
np.PointID = Convert.ToInt64(1235);
np.TypeCode = Convert.ToInt64(66);
arr[1] = np;
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
byte[] temp = new byte[structSize * arr.Length];
byte[] temp1 = Struct2Byte(arr[0]);
byte[] temp2 = Struct2Byte(arr[1]);
Array.Copy(temp1, 0, temp, 0, temp1.Length);
Array.Copy(temp2, 0, temp, structSize, temp2.Length);
WriteInfo(temp);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] bt = ReadInfo(filename);
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
int num = bt.Length / structSize;
for (int i = 0; i < num; i++)
{
byte[] temp = new byte[structSize];
Array.Copy(bt, i * structSize, temp, 0, structSize);
MY_STRUCT np = new MY_STRUCT();
np = Byte2Struct(temp);
}
}
}
}
C#中将结构类型数据存储到二进制文件中方法的更多相关文章
- MVC中将枚举类型数据应用到下拉列表中的方法
例如: public enum ItemTypes { Movie = 1, Game = 2, Book = 3 } 在MVC2.0中如何将以上枚举类型使 ...
- MySQL 5.7:非结构化数据存储的新选择
本文转载自:http://www.innomysql.net/article/23959.html (只作转载, 不代表本站和博主同意文中观点或证实文中信息) 工作10余年,没有一个版本能像MySQL ...
- 整型变量修饰符,char类型数据存储原理,字节数,
//------------------整型变量修饰符 修饰符(int short long longlong signed unsigned)所有修饰符都是用来修整形 int 4short %hd ...
- IOS开发数据存储篇—IOS中的几种数据存储方式
IOS开发数据存储篇—IOS中的几种数据存储方式 发表于2016/4/5 21:02:09 421人阅读 分类: 数据存储 在项目开发当中,我们经常会对一些数据进行本地缓存处理.离线缓存的数据一般都 ...
- Android数据存储的五种方法汇总
本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用SharedPreferences存储数据 2 ...
- DELPHI中枚举类型数据的介绍和使用方法
在看delphi程序的时候看到aa=(a,b,c,d);这样的东西,还以为是数组,同事说是函数,呵呵,当然这两个都不屑一击,原来这样式子是在声明并付值一个枚举类型的数据.下边写下来DELPHI中枚举类 ...
- Android之数据存储的五种方法
1.Android数据存储的五种方法 (1)SharedPreferences数据存储 详情介绍:http://www.cnblogs.com/zhangmiao14/p/6201900.html 优 ...
- Java基础知识强化之IO流笔记46:IO流练习之 把文本文件中数据存储到集合中的案例
1. 把文本文件中数据存储到集合中 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合. 分析: 通过题目的意思我们可以知道如下的一些内容, 数据 ...
- JAVA之旅(十七)——StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder
JAVA之旅(十七)--StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder 讲完String,我们来聊聊他的小兄弟 一.StringB ...
随机推荐
- Visual studio 非常好的插件
1. Productive power tools2015 2. Visual studio spell checker
- mac下java 开发环境搭建
mac配置java开发环境: jdk1.7 +sdk1.7+maven +tomcat 1.先安装jdk ,才能安装sdk . 2 mac中jdk1.7的默认位置:/Library/Java/Ja ...
- 『TCP/IP详解——卷一:协议』读书笔记——08
2013-08-21 13:56:23 3.3 IP路由选择 1. IP路由选择有两种情况.(1)如果目的主机与源主机直接相连(如点对点链路)或都在一个共享网络上(以太网或令牌环网),那么IP数据报就 ...
- ARCGIS FOR JAVASCRIPT API 出现multipleDefine问题
问题: Error {src: "dojoLoader", info: Object, stack: (...), message: "multipleDefine&qu ...
- Idol之坑
1. 编码格式不统一 Idol的API 是基于 Http 的 Get 调用,输入N多参数,可以吐出Xml和 Json. 由于Idol没有在输出文档中指定编码格式(ContentType),用 Http ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
- [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]
Very often, especially in programming contests, we treat a sequence of non-whitespace characters as ...
- [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)
Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...
- tomcat通过conf-Catalina-localhost目录发布项目详解
Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需 ...
- Android SDK国内代理速度还可以
Android Android SDK 配置步骤 启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manag ...