4个回答
展开全部
1.读取某个指定字节
Private Sub Command1_Click()
Dim a() As Byte, b As Byte, i As Integer, j, S As String
'S = App.Path & "\1.txt" '请修改为实际文件名
S = "C:\Documents and Settings\Administrator\桌面\1.txt"
ReDim a(FileLen(S))
Open S For Binary As #1 '打开文件
'下面演示指针的移动
Seek #1, 10 '指针移到第10个字节
Get #1, , b
MsgBox "第10个字节:" & b
Seek #1, 23 '指针移到第23个字节
Get #1, , b
MsgBox "第23个字节:" & b
'下面演示始一个一个地读出文件
Seek #1, 1 '指针返回到文件首
For i = 1 To FileLen(S)
Get #1, , b '读取1个字节
a(i - 1) = b '存入数组
Next
Close #1 '关闭文件
S = StrConv(a, vbUnicode)
MsgBox S
End Sub
2读取某行
Function lines(txtpath As String, ByVal startline As Integer, linenum As Integer) As String '显示 txtpath 文件的从startline 行开始的 linenum 行的内容
lines = ""
Dim filetxt As String, x As Variant, i As Integer
filetxt = String(FileLen(txtpath), " ")
Open txtpath For Binary As 1
Get #1, , filetxt
Close 1
x = Split(filetxt, vbCrLf)
msgbox ubound(x)+1 '行数
If startline > UBound(x) Then MsgBox "行溢出", 64, "err!": Exit Function
If startline <= UBound(x) Then
If startline + linenum <= UBound(x) Then
For i = startline To startline + linenum - 1
lines = lines & x(i) & " "
Next
Else
For i = startline To UBound(x)
lines = lines & x(i) & " "
Next
End If: End If
End Function
.
网上的代码,这类型的很多,
不外乎就是
设定读取路径再
Open S For Binary As #1找开(注意OPEN的参数有几种类型)
再GET
Private Sub Command1_Click()
Dim a() As Byte, b As Byte, i As Integer, j, S As String
'S = App.Path & "\1.txt" '请修改为实际文件名
S = "C:\Documents and Settings\Administrator\桌面\1.txt"
ReDim a(FileLen(S))
Open S For Binary As #1 '打开文件
'下面演示指针的移动
Seek #1, 10 '指针移到第10个字节
Get #1, , b
MsgBox "第10个字节:" & b
Seek #1, 23 '指针移到第23个字节
Get #1, , b
MsgBox "第23个字节:" & b
'下面演示始一个一个地读出文件
Seek #1, 1 '指针返回到文件首
For i = 1 To FileLen(S)
Get #1, , b '读取1个字节
a(i - 1) = b '存入数组
Next
Close #1 '关闭文件
S = StrConv(a, vbUnicode)
MsgBox S
End Sub
2读取某行
Function lines(txtpath As String, ByVal startline As Integer, linenum As Integer) As String '显示 txtpath 文件的从startline 行开始的 linenum 行的内容
lines = ""
Dim filetxt As String, x As Variant, i As Integer
filetxt = String(FileLen(txtpath), " ")
Open txtpath For Binary As 1
Get #1, , filetxt
Close 1
x = Split(filetxt, vbCrLf)
msgbox ubound(x)+1 '行数
If startline > UBound(x) Then MsgBox "行溢出", 64, "err!": Exit Function
If startline <= UBound(x) Then
If startline + linenum <= UBound(x) Then
For i = startline To startline + linenum - 1
lines = lines & x(i) & " "
Next
Else
For i = startline To UBound(x)
lines = lines & x(i) & " "
Next
End If: End If
End Function
.
网上的代码,这类型的很多,
不外乎就是
设定读取路径再
Open S For Binary As #1找开(注意OPEN的参数有几种类型)
再GET
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Type aatype
a As String * 2
b As String * 2
c As String * 2
d As String * 2
End Type
Dim aa As aatype
Dim bb As Integer
Private Sub Command1_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "插入数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
Text5 = tt
For i = tt To bb Step -1
Get #1, i, aa
Put #1, i + 1, aa
Next
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, bb, aa
Close #1
End Sub
Private Sub Command2_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "删除数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
For i = bb To tt
Get #1, i + 1, aa
Put #1, i, aa
Next
Close #1
End Sub
Private Sub Command3_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "读取数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
Get #1, bb, aa
Text1 = aa.a
Text2 = aa.b
Text3 = aa.c
Text4 = aa.d
Close #1
End Sub
Private Sub Command4_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "修改数据")
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, lastrecord + bb, aa
Close #1
End Sub
Private Sub Command5_Click()
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, tt + 1, aa
Close #1
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
End Sub
我也是仿别人的做的.
a As String * 2
b As String * 2
c As String * 2
d As String * 2
End Type
Dim aa As aatype
Dim bb As Integer
Private Sub Command1_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "插入数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
Text5 = tt
For i = tt To bb Step -1
Get #1, i, aa
Put #1, i + 1, aa
Next
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, bb, aa
Close #1
End Sub
Private Sub Command2_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "删除数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
For i = bb To tt
Get #1, i + 1, aa
Put #1, i, aa
Next
Close #1
End Sub
Private Sub Command3_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "读取数据")
If bb = 0 Then Exit Sub
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
Get #1, bb, aa
Text1 = aa.a
Text2 = aa.b
Text3 = aa.c
Text4 = aa.d
Close #1
End Sub
Private Sub Command4_Click()
On Error Resume Next ' Resume重新开始; (经打断后)再继续
bb = InputBox("输入记录号", "修改数据")
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, lastrecord + bb, aa
Close #1
End Sub
Private Sub Command5_Click()
Open "d:\aa.txt" For Random As #1 Len = Len(aa)
tt = LOF(1) / Len(aa)
aa.a = Text1.Text
aa.b = Text2.Text
aa.c = Text3.Text
aa.d = Text4.Text
Put #1, tt + 1, aa
Close #1
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
End Sub
我也是仿别人的做的.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
读取:
Open "我的文件.txt" For Input As #1
'用Input #1,变量表 来输入变量的值
'也可以用Line Input #1,temp输入一行到temp中
'文件不存在或者拒绝访问的时候会产生错误
Close #1
写入(覆盖性):
Open "我的文件.txt" For Output As #1
'用Print #1,参量表 来输出
'文件不存在的时候自动创建,拒绝访问的时候会产生错误
Close #1
追加:
Open "我的文件.txt" For Append As #1
'用Print #1,参量表 来输出到文件末尾
'文件不存在的时候自动创建,拒绝访问的时候会产生错误
Close #1
二进制改写:
Open "我的文件.txt" For Binary As #1
'用Get #1, k, temp 读取第k字节的数值到temp,注意:1<=k<=文件长度
'用Put #1, k, temp 保存第k字节的数值为temp,注意:1<=k<=文件长度
'另外,这样定义temp: Dim temp As Byte
'有的时候会产生错误(例如:拒绝访问)
Close #1
Open "我的文件.txt" For Input As #1
'用Input #1,变量表 来输入变量的值
'也可以用Line Input #1,temp输入一行到temp中
'文件不存在或者拒绝访问的时候会产生错误
Close #1
写入(覆盖性):
Open "我的文件.txt" For Output As #1
'用Print #1,参量表 来输出
'文件不存在的时候自动创建,拒绝访问的时候会产生错误
Close #1
追加:
Open "我的文件.txt" For Append As #1
'用Print #1,参量表 来输出到文件末尾
'文件不存在的时候自动创建,拒绝访问的时候会产生错误
Close #1
二进制改写:
Open "我的文件.txt" For Binary As #1
'用Get #1, k, temp 读取第k字节的数值到temp,注意:1<=k<=文件长度
'用Put #1, k, temp 保存第k字节的数值为temp,注意:1<=k<=文件长度
'另外,这样定义temp: Dim temp As Byte
'有的时候会产生错误(例如:拒绝访问)
Close #1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上说的已经很详细了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询