导航:首页 > 文件教程 > vb读写文件

vb读写文件

发布时间:2023-02-24 14:40:38

Ⅰ vb 二进制文件读写

Dim bytes() As Byte
dim lLength as Long
Open "c:\main.exe" For Binary As #1 '读取字节
lLength=LOF(1)
ReDim bytes(1 To lLength) As Byte
Get 1, , bytes
Close #1

'将文件以十六进制方式输出到Text1中,这个过程很耗时,130K的文件大概需要2分钟,而且越到后面速度越慢
Dim strTmp As String
For i = 1 To lLength
strTmp = strTmp & " " & Hex(bytes(i))
DoEvents
Next
Text1.Text = strTmp

Open "d:\abcd.exe" For Binary As #1 '将字节转存
Put 1, , bytes
Close #1

End Sub

Ⅱ VB读写INI文件

'API函数的声明
'ini配置文件
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

'自定义函数
'读取/设置ini配置文件
Private Function GetIniS(FilePath As String, SectionName As String, KeyWord As String, DefString As String) As String
Dim ResultString As String * 255, Temp As Long
Dim s As String, i As Integer
Temp = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 255, FilePath)
If Temp > 0 Then '关键词的值不为空
s = ""
For i = 1 To 255
If Asc(Mid(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid(ResultString, i, 1)
End If
Next i
Else
If DefString <> "" Then
'将缺省值写入INI文件
Temp = WritePrivateProfileString(SectionName, KeyWord, DefString, FilePath)
s = DefString
End If
End If
GetIniS = s
End Function

Private Function GetIniN(FilePath As String, SectionName As String, KeyWord As String, DefValue As Long) As Long
Dim d As Long, s As String
d = DefValue
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, FilePath)
'If d <> DefValue Then
' s = CStr(d)
' d = WritePrivateProfileString(SectionName, KeyWord, s, FilePath)
'End If
End Function

Private Sub SetIniS(FilePath As String, SectionName As String, KeyWord As String, ValStr As String)
Dim res As Long
res = WritePrivateProfileString(SectionName, KeyWord, ValStr, FilePath)
End Sub

Private Sub SetIniN(FilePath As String, SectionName As String, KeyWord As String, ValLon As Long)
Dim res As Long, s As String
s = CStr(ValLon)
res = WritePrivateProfileString(SectionName, KeyWord, s, FilePath)
End Sub

'读取过程
Private Sub Command1_Click()
Dim fname As String
Dim SectionName As String, KeyWord As String, DefString As String, DefValue As Long
Dim s As String, sValue As String
Dim i As Integer, j As Integer
Dim Result() As String

fname = "......" '你所要读取的文件(包括其绝对路径)
SectionName = "KeyName" '字段
KeyWord = "Key" '相同的部分
DefString = "???" '一般为空字符,我这里是用于区分Value1为空是是否继续进行用的

i = 0
j = -1
Do
i = i + 1
s = KeyWord & CStr(i)
sValue = GetIniS(fname, SectionName, KeyWord, DefString)
If sValue <> DefString Then
j = j + 1
ReDim Preserve Result(j)
Result(j) = sValue
Else
Exit Do
End If
Loop

End Sub

你再结合自己的具体情况,修改下就行了

Ⅲ 如何用vb写文件操作

1、Open 文件名 [For方式] [Access存取类型] [锁定] AS [#]文件号 [Len=记录长度]
功能:为文件的输入输出分配缓冲区,并确定缓冲区所使用的存取方式
说明:
1) 方式:指定文件的输入输出方式,可选,默认是Random,可以是以下值
a、 Output:指定顺序输出方式,将覆盖原有内容
b、 Input:指定顺序输入方式
c、 Append:指定顺序输出方式,在文件未尾追加内容
d、 Random:指定随机存取方式,也是默认方式,在Random方式时,如果没有Access子句,则在执行Open语句时,VB将按下列顺序打开文件:读/写、只读、只写
e、 指定二进制文件。在这种方式下,可以用Get和Put语句对文件中任何字节位置的信息进行读写。在Binary方式中,如果没有Access子句,则打开文件的类型与Random方式相同
2)、存取类型:放在关键字Access之后,用来指定访问文件的类型。可以是下列类型之一
a、 Read:打开只读文件
b、Write:打开只写文件
c、 Read Write:打开读写文件。这种类型只对随机文件、二进制文件及用Append方式打开的文件有效
3)、锁定:该子句只在多用户或多进和环境中使用,用来限制其他用户或其他进程对打开进行读写操作。锁定类型包括:
a、 默认:如不指定锁定类型,则本进程可以多次打开文件进行读写;在文件打开期间,其他进程不能对该文件执行读写操作
b、Lock Shared:任何机器上的任何进程都可以对该文件进行读写操作
c、 Lock Read:不允许其他进程读该 文件。只在没有其他Read存取类型的进程访问该文件时,才允许这种锁定。
d、Lock Write:不允许其他进程写这个文件。只在没有其他Write存取类型的进程访问该文件时,才允许这种锁定
e、 Lock Read Write:不允许其他进程读写这个文件
如果不使用lock子句,则默认为Lock Read write
4)、文件号:由用户自行指定一个由1~511之间的整数,只要该文件号未被使用就合法;打开文件后,可以用该文件号进行读写等操作
5)、记录长度:是一个整型表达式。当选择该参量时,为随机存取文件设置记录长度。对于用随机访问方式打开的文件,该值是记录长度;对于顺序文件,该值是缓冲字符数。”记录长度”不能超过32767字节。对于二进制文件,将忽略Len子句
举例:Open “price.dat” for Output as #1
Open “C:\abc.dat” for radom as #1 len=256
2、Close [#文件号][,#文件号]……:关闭文件
3、Seek #文件号,位置:文件指针跳到指定位置,以字节为单位。取值1~pow(2,31)-1
4、Seek (文件号):返回当前文件指针的位置
5、FreeFile():取得一个未使用的文件号
6、Loc(文件号):返回指定文件的当前读写位置
7、LOF(文件号):返回文件长度
8、EOF(文件号):用来测试文件是否结束,结束返回true
9、Print #文件号,变量1,变量2,…变量n:按顺序将各变量的值写入顺序文件
如果是print #文件号,则写入空行
10、 Write #文件号,表达式表…:作用同 Print
11、 Input #文件号,变量表….:读顺序文件,进行与Print相反的操作
12、 Line Input #文件号,字符串变量:从顺序文件中读入一行
13、 Input$(n,#文件号):从顺序文件读出 n个字符的字符串
14、 Put #文件号,[记录号],变量:把除对象变量和数组变量外的任何变量(包括号含有单个数组元素的下标变量)的内容写入随机文件。
例如:Put #2,,filebuff
15、 Get #文件号,[记录号],变量:读随机文件,执行与put相反的操作
16、 Get|put #文件号,[位置],变量:读写二制文件,位置是指下一次读写操作的位置。
17、 Kill 文件名:删除文件
18、 FileCopy 源文件名,目标文件名:复制文件
19、 Name原文件名 as 新文件名:重命令文件

Ⅳ VB.NET 怎么读写二进制文件,类似Open

'从文件指定位置读取数据
dim fn as new io.filestream("e:\123.mp3", IO.FileMode.Open)
dim fr as new io.binaryreader(fn)
fr.BaseStream.Position = 1000 '从1000字节处开始读取文件
dim data() as byte= fr.ReadBytes(2000) '读取2000个字节至data()数组
fr.close
fn.close

'在文件指定位版置写权入数据
dim fn as new io.filestream("e:\123.mp3", IO.FileMode.Open)
dim fr as new io.binarywriter(fn)
fr.BaseStream.Position = 1000 '从1000字节处开始读取文件
fr.write(data) '将data数组写入文件
fr.close
fn.close

'关键在于这句:fr.BaseStream.Position = 1000 ,即将文件流的指针移动到1000字节处,然后再根据自己的要求操作即可。

Ⅳ vb.net二进制怎么读取文件

一、二进制文件读写

1、写二进制数据到指定目录

Ⅵ VB中如何读写文件 谢谢了

用open
open <文件名>[for 打开方式] as [#]<文件号>
打开方式有三种
(1)input:向计算机输入数据,即从所打开的文件中读取数据。
(2)output:向文件写数据,即从计算机向所打开的文件写数据。如果文件中原来已有数据,则原有数据被抹去,即新写上的数据覆盖已有的数据。
(3)append:向文件添加数据,即从计算机向所打开的文件写数据。不同于output方式的是,append方式把新的数据添加到文件尾部原有数据的后边,文件中保留原有数据。
AS是一个关键字,as引导的短语为打开的文件指定一个文件号。#号是可选项。文件号是一个1~511之间的整数。文件号用来代表所打开的文件。

例如:打开c:\a.txt文件
open "c:\a.txt" for output as #1

向文件写数据用print和write语句:
print #<文件号>[,输出表列]
write #<文件号>〔,输出表列]
例如向上面打开的文件写数据
print #1,"a";"b";"c"
close #1
文件里显示为:
abc

Ⅶ VB 怎样将文本框的数据读取和保存到文本文件中

VB将文本框的数据读取和保存到文本文件中,实际上就是VB对txt的读写操作。
相关代码如下:
1、vb读取txt文件内容,以下代码实现把电脑txt中的内容读取到程序文本框text1中。
Private Sub Command1_Click()
Open App.Path & "\123.txt" For Input As #1 '备注:App.Path & "\123.txt"是程序目录下的123.txt
Dim Lines As String
Line Input #1, Lines '读取文本内容
Close #1
text1.Text = Lines
End Sub
2、vb写入txt内容,实现文本框的数据写入到文本文件中。
Private Sub Command2_Click()
Dim a As String
a = text1.Text '先读取出text1内容
Open App.Path & "\123.txt" For Output As #1
Print #1, a '把a的值写入123.txt
Close #1
End Sub

阅读全文

与vb读写文件相关的资料

热点内容
类似安全文件的软件 浏览:723
为什么编程要学这门语言 浏览:678
网卡的配置文件目录 浏览:960
计算器单片机编程叫什么 浏览:736
网站编辑需要会什么 浏览:622
单片机程序计数器的功能 浏览:917
网络摄像头怎么接虚拟储存器 浏览:84
好压IMG压缩文件转换成光盘 浏览:588
前端json格式如何排序 浏览:821
ocx实现选择保存文件 浏览:986
office2007教程免费下载 浏览:489
pop编程是什么 浏览:760
三星升级系统联接失败 浏览:929
微信忘记独立密码 浏览:555
spike如何编程 浏览:503
javajlabel位置 浏览:728
如何用大数据做企业 浏览:656
复合编程用什么软件最好 浏览:505
txt转换word 浏览:797
java字面值的存储 浏览:261

友情链接