VB中如何把2个文本文件中的内容合并到一个文本文件内?

2024-12-03 14:50:01
推荐回答(3个)
回答1:

Private Sub Command1_Click()
Dim i&, arr$(2)
For i = 1 To 3
ReadFrom i & ".txt", arr(i - 1)
Next
WriteTo "4.txt", Join$(arr, vbCrLf)
MsgBox "ok", 64
End Sub

Sub ReadFrom(FileName$, TXT$)
Dim fn&: fn = FreeFile
Open FileName For Input As #fn
TXT = StrConv(InputB$(LOF(fn), fn), vbUnicode)
Close #fn
End Sub

Sub WriteTo(FileName$, TXT$)
Dim fn&: fn = FreeFile
Open FileName For Binary As #fn
Put #fn, , TXT
Close #fn
End Sub

回答2:

DIM B AS String
DIM C As String
OPEN "1.TXT" FOR Input AS #1
OPEN "2.TXT" FOR Input AS #2
OPEN "3.TXT" FOR Input AS #3
OPEN "4.TXT" FOR OutPut AS #4
Line Input #1,B
C=B & VBCRLF
Line Input #2,B
C=C & B & VBCRLF
Line Input #3,B
C=C & B & VBCRLF
Print #1,C
CLOSE #4
Close #3
Close #2
Close #1

回答3:

Dim S As String
Open "H:\1.TXT" For Input As #1
Open "H:\2.TXT" For Input As #2
Open "H:\3.TXT" For Input As #3
Open "H:\4.TXT" For Output As #4
While Not EOF(1)
Line Input #1, S
Print #4, S
Wend
While Not EOF(2)
Line Input #2, S
Print #4, S
Wend
While Not EOF(3)
Line Input #3, S
Print #4, S
Wend
Close #1
Close #2
Close #3
Close #4