在vb中怎么将数据以二进制的形式存入txt文件

twohorses 2010-05-28 09:56:07
我在txt文件里存入大量的数据(绘制曲线用),由于数据比较多存入txt以后,文件有2M左右,现在想以二进制的形式存入txt,这样文件就比较小,请问怎么实现,另外存入以后怎么能一个一个读出来?
...全文
161 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
twohorses 2010-05-28
  • 打赏
  • 举报
回复
以二进制存入,和以文本存,文件所占得的大小不一样吗?其实质不都是最终以二进制存的吗?那它俩之间到底有什么区别
赵4老师 2010-05-28
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Dim f As Integer
Dim i As Long
Dim d As Double
Dim pos As Long
f = FreeFile()
Open "c:\data.bin" For Binary Access Write As #f
For i = 0 To 99
d = i / 100#
Put #f, , d
Next
Close #f
Open "c:\data.bin" For Binary Access Read As #f
Debug.Print "LOC(f)="; Loc(f)
Get #f, , d
Debug.Print Format(d, "0.00")
Debug.Print "LOC(f)="; Loc(f)
Get #f, , d
Debug.Print Format(d, "0.00")
Debug.Print "LOC(f)="; Loc(f)
Seek #f, 1& + 12 * 8& '读第12个Double数
Debug.Print "LOC(f)="; Loc(f)
Get #f, , d
Debug.Print Format(d, "0.00")
Debug.Print "LOC(f)="; Loc(f)
Close #f
End Sub
thllv 2010-05-28
  • 打赏
  • 举报
回复
lz要是电脑专业的这个问题不该不会吧
大虾们估计懒得回答你,我说两句

pc机最小的可寻址内存单元为一个字节(8位bit,一个字节),对应c中char类型,范围0-255。一个字长为两个字节
但为了快速寻址一般为双字(DWORD)对齐,即常说的32位系统。
一个DWORD用于存无符号数字范围0-4294967295(32位bit,4个字节)
而“4294967295”你要用txt按十进制来存就需要10个char,即10个字节

应该能看出差别了
要存浮点数差别就更大了
图片的常见存储与读取凡是有以下几种: 存储图片:以二进制形式存储图片时,要把数据的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]. 1.参数是图片路径:返回Byte[]类型: public byte[] GetPictureData(string imagepath) { /**/////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; }2.参数类型是Image对象,返回Byte[]类型: public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //将Image转换成流数据,并保存为byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }好了,这样通过上面的方法就可以把图片转换成Byte[]对象,然后就把这个对象保存到数据去就实现了把图片的二进制格式保存到数据去了。下面我就谈谈如何把数据的图片读取出来,实际上这是一个相反的过程。 读取图片:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX 1.参数是Byte[]类型,返回值是Image对象: public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }2.参数是Byte[] 类型,没有返回值,这是针对asp.net把图片从输出到网页上(Response.BinaryWrite) public void WritePhoto(byte[] streamByte) { // Response.ContentType 的默认值为默认值为“text/html” Response.ContentType = "image/GIF"; //图片输出的类型有: image/GIF image/JPEG Response.BinaryWrite(streamByte); }补充: 针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型: Response.ContentType = "application/msword"; Response.ContentType = "application/x-shockwave-flash"; Response.ContentType = "application/vnd.ms-excel";另外可以针对不同的格式,用不同的输出类型以适合不同的类型: switch (dataread("document_type")) { case "doc": Response.ContentType = "application/msword"; case "swf": Response.ContentType = "application/x-shockwave-flash"; case "xls": Response.ContentType = "application/vnd.ms-excel"; case "gif": Response.ContentType = "image/gif"; case "Jpg": Response.ContentType = "image/jpeg"; }

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧