用vb如何如何自动清除文本文件中的空行?

或者用文本框打开一个文本文件时自动清除文本它里面的空行
2025-02-14 18:28:01
推荐回答(1个)
回答1:

Private Sub Command1_Click()
Dim tx() As String, tmp As String, n As Integer
'首先将txt文件里面非空行的内容读进数组tx里
Open "d:\a.txt" For Input As #1
n = 0
Do While Not EOF(1)
Line Input #1, tmp
If tmp <> "" Then
ReDim Preserve tx(n)
tx(n) = tmp
n = n + 1
End If
Loop
Close #1
'然后就清空txt文件,把数组tx里面的内容写进去就可以了
Open "d:\a.txt" For Output As #2
Cls
For i = 0 To UBound(tx)
Print #2, tx(i)
Next
Close #2

'如果你是要将过滤了空行的内容读进文本框里面,那么:
For i = 0 To UBound(tx)
Text1.Text = Text1.Text & tx(i) & vbCrLf
Next
End Sub