将二进制文件读取到VB2008中的结构 [英] Read a binary file to a structure in VB2008

查看:71
本文介绍了将二进制文件读取到VB2008中的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用VB6编写的程序.我正在尝试将其升级到VB2008. VB6程序使用一个二进制数据文件,其中包含来自用户定义类型的记录.我可以在vb6中一步打开东西并将其读入数据数组:

I have a program that I wrote in VB6. I''m attempting to upgrade it to VB2008. The VB6 program uses a binary data file full of records from a user-defined type. I was able to open the thing and read it into the data array in one step in vb6:

Private Type eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
End Type

Dim datalist() as eRecord


Open Datfile for Binary as #fnum
Get #fnum, , reclen
Redim datalist(reclen)
Get #fnum, , datalist
Close fnum


完毕.像馅饼一样容易

现在,我已经花了一个半小时在youtube视频和代码网站中进行搜索,以寻找如何在VB2008中执行此操作,而我从来没有使用过任何解释.我已经建立了数据列表"的结构,这是我所掌握的. (有一个自动升级vb6代码的功能,但是失败了.所以我从头开始.)

无论如何,将不胜感激一个简单易懂的代码段.提前谢谢.


Done. Easy as pie

Now I''ve spent the past hour and a half searching through youtube videos and code websites looking for how to do this in VB2008 and there''s never an explanation I can use. I''ve got the structure for "datalist" set up, and that''s as far as I got. (There''s a function to automatically upgrade vb6 code, but that failed. So I''m starting from scratch.)

Anyway, a simple, easy-to-understand code snippet would be greatly appreciated. Thanks in advance.

Structure eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
end Stucture

Dim datalist() as eRecord

推荐答案

到目前为止,这不是一种很好的数据存储方法.问题不在于数据本身的二进制表示形式,而是它的即席和非灵活性质.

无论如何,您需要使用类System.IO.BinaryReaderSystem.IO.BinaryWriter:
http://msdn.microsoft.com/en-us/library/system.io. binaryreader.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.io. binarywriter.aspx [ ^ ].

基本上,您需要按组件读取和写入结构:
This is by far not a nice approach to data storage. The problem is not the binary representation of data itself, but it''s ad-hoc and non-flexible nature.

Anyway, you need to use the classes System.IO.BinaryReader and System.IO.BinaryWriter:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^].

Basically, you would need to read and write the structure by component:
using System.IO;

//...

BinaryReader reader = new BinaryReader(/* ... */);
//...
eRecord myRecord = //...

// your structure fields are non-public; you need to change them into public or internal,
// or, better, represent them via public or internal properties; thank you can do this:

// read date here, see below
myRecord.fName = reader.ReadString();
myRecord.lName = reader.ReadString();
myRecord.dep = reader.ReadString();



我不知道您是如何表示日期的,所以很可能需要将日期添加到一些基本类型的组件(例如某些整数类型)中,并使用reader.ReadUint32reader.ReadUInt16逐一读取, reader.ReadByte等.您可以使用某些二进制编辑器进行检查,以查看其二进制结构或从原始代码中找出.

BinaryWriter以与阅读对称的方式以相同的方式进行书写.

下次,不要做这些事情.改为学习序列化:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework [ http://msdn.microsoft.com/en-us/library/ms233843.aspx [ ^ ].

首先,作为一种非常易于使用,强大且通用的序列化方法,请考虑使用 Data Contracts :
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

祝你好运,

—SA



I don''t know how you represented the date, so chances are, you would need to bread date into some primitive-type components like some integer types and read the one-by-one, using reader.ReadUint32, reader.ReadUInt16, reader.ReadByte or the like. You can check with some binary editor to see its binary structure or figure out from original code.

Writing is done in the same way with BinaryWriter, symmetrical to reading.

Next time, don''t do such things. Learn serialization instead:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^].

First of all, as a very easy-to-use, robust and universal approach to serialization, consider using Data Contracts:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Good luck,

—SA


这篇关于将二进制文件读取到VB2008中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆