求VB达人指点实现TXT文档中的查找与替换问题?!
条件如下:1.TXT内容:1,1234562,7891233,中国4,日本2.TXT内容:1,手机1,电话1,无线电2,东京2,北京3,台北比对2.TXT查找1.TXT文...
条件如下:
1.TXT内容:
1,123456
2,789123
3,中国
4,日本
2.TXT内容:
1,手机
1,电话
1,无线电
2,东京
2,北京
3,台北
比对2.TXT查找1.TXT文档中的是否有1,如果有则将1.TXT内中的1,後123456此内容
取代掉2.TXT文内中所有的1变成123456,并另存为3.TXT.
注:123456此内容并不一定都一样是变数.
请VB高手详解提供语法一下,谢谢感恩了!!~(新手学习)
我试写了以下语法,但不行会报错"输入已超过档案结尾"
Dim a, b, c() As String, d() As String
Open "D:\1.txt" For Input As #1
Open "D:\2.txt" For Input As #2
Open "D:\3.txt" For Output As #3
a = Input(LOF(1), #1)
b = Input(LOF(1), #2)
c = Split(a, ",")
d = Split(b, ",")
If c(0) = 1 And d(0) = 1 Then
e = Replace(c(0), 1, d(1))
msgbox e & "," & c(1)
Print #3, e & "," & c(1)
Else
msgbox "没资料"
GoTo ED
End If
ED:
Close #1
Close #2
Close #3
msgbox "作业完成 D:\3.txt " 展开
1.TXT内容:
1,123456
2,789123
3,中国
4,日本
2.TXT内容:
1,手机
1,电话
1,无线电
2,东京
2,北京
3,台北
比对2.TXT查找1.TXT文档中的是否有1,如果有则将1.TXT内中的1,後123456此内容
取代掉2.TXT文内中所有的1变成123456,并另存为3.TXT.
注:123456此内容并不一定都一样是变数.
请VB高手详解提供语法一下,谢谢感恩了!!~(新手学习)
我试写了以下语法,但不行会报错"输入已超过档案结尾"
Dim a, b, c() As String, d() As String
Open "D:\1.txt" For Input As #1
Open "D:\2.txt" For Input As #2
Open "D:\3.txt" For Output As #3
a = Input(LOF(1), #1)
b = Input(LOF(1), #2)
c = Split(a, ",")
d = Split(b, ",")
If c(0) = 1 And d(0) = 1 Then
e = Replace(c(0), 1, d(1))
msgbox e & "," & c(1)
Print #3, e & "," & c(1)
Else
msgbox "没资料"
GoTo ED
End If
ED:
Close #1
Close #2
Close #3
msgbox "作业完成 D:\3.txt " 展开
展开全部
Dim a1() As String, a2() As String, s As String, i As Long
Open "D:\2.txt" For Binary As #1
s = Input(LOF(1), #1)
Close #1
Open "D:\1.txt" For Binary As #1
a1 = Split(Input(LOF(1), #1), vbCrLf)
Close #1
For i = 0 To UBound(a1)
a2 = Split(a1(i), ",")
If UBound(a2) > 0 Then s = Replace(s, a2(0) & ",", a2(1) & ",")
Next
Open "D:\3.txt" For Binary As #1
Put #1, , s
Close #1
MsgBox "作业完成 D:\3.txt"
Open "D:\2.txt" For Binary As #1
s = Input(LOF(1), #1)
Close #1
Open "D:\1.txt" For Binary As #1
a1 = Split(Input(LOF(1), #1), vbCrLf)
Close #1
For i = 0 To UBound(a1)
a2 = Split(a1(i), ",")
If UBound(a2) > 0 Then s = Replace(s, a2(0) & ",", a2(1) & ",")
Next
Open "D:\3.txt" For Binary As #1
Put #1, , s
Close #1
MsgBox "作业完成 D:\3.txt"
更多追问追答
追问
前辈可否每行注解说明一下,小弟新手想了解其逻辑性,劳烦您了,感恩!!~
追答
Dim a1() As String, a2() As String, s As String, i As Long
Open "D:\2.txt" For Binary As #1 '把2.txt按二进制方式打开,可实现精确操作,下同
s = Input(LOF(1), #1) '把2.txt的全部内容读入变量s
Close #1
Open "D:\1.txt" For Binary As #1
a1 = Split(Input(LOF(1), #1), vbCrLf) '把1.txt读入并按行分解为数组a1
Close #1
For i = 0 To UBound(a1) '循环读取数组a1
a2 = Split(a1(i), ",") '把每行按逗号分解为两部分,如a2(0)为1,a2(1)为123456
If UBound(a2) > 0 Then s = Replace(s, a2(0) & ",", a2(1) & ",") '把s中的所有和a2(0)相等的部分替换为a2(1)
Next '继续读取a1中的下一个数(或者说是2.txt的下一行)
Open "D:\3.txt" For Binary As #1
Put #1, , s '把替换后的s存入3.txt
Close #1
MsgBox "作业完成 D:\3.txt"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询