两个文本文件如何按要求合并?
两个文本文件,A和B,A里面有40000条数据,B里面有28000条数据,都是一条一行,数据格式都一样:“a,b,c”。现在想从A文本里开始读起,读一条,就在B文本里搜寻前两个数字(a和b)一样的一条数据,如果找不到就在新建的文本文件C里写入A文件里的这一条,如果找到了,就在新建的文本文件C里写入B文件里的这一条(相当于用B文件里的这一条替换A文件里的这一条),最后,C文件里的条目数和A文件一致,C文件里的排列顺序和A文件一致。我编写的程序如下:(问题:1、慢,想想要循环40000*28000次,10亿多次呢!怎么解决?
2、好象逻辑有错误,转出来的C文件与A文件完全一致,肯定应该不一致。)
程序代码:
Dim fn As Integer, str1 As String, str2 As String, tmp1, tmp2, i, j As Long Dim a() As String Dim b() As String Dim cc As String fn = FreeFile Open "D:\A.txt" For Binary As #fn str1 = Input(LOF(fn), fn) tmp1 = Split(str1, vbCrLf, -1) Close #fn fn = FreeFile Open "D:\B.txt" For Binary As #fn str2 = Input(LOF(fn), fn) tmp2 = Split(str2, vbCrLf, -1) Close #fn Open "D:\C.txt" For Append As #2 For i = LBound(tmp1) To UBound(tmp1) - 1 For j = LBound(tmp2) To UBound(tmp2) - 1 a = Split(tmp1(i), ",") b = Split(tmp2(j), ",") If a(0) = b(0) And a(1) = b(1) Then cc = tmp2(j) Else cc = tmp1(i) End If Next j If cc <> "" And aa <> "" Then Print #2, cc End If Next i Close #2
文件:
A.rar
(41.12 KB)
B.rar
(30.84 KB)
请版主和路过的坛友及高手不吝赐教,多谢多谢!