错误的原因是后面两个EOF(1),前面已经将#1文件关闭了。后面打开的是#2,应该改成EOF(2)。
另外,要注意应用程序放在根目录的情况,此时的app.path是"c:\",那么你的打开的文件路径就成了"c:\\2.txt“,会导致出错。
Private Sub oneoneone_Click()
Text1.Text = ""
Open "C:\Documents and Settings\Administrator\桌面\GIS vb实验一原始资料\SZTJ.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, str1ine
Text1.Text = Text1.Text + str1ine + vbCrLf
Loop
Close #1
MsgBox "打开成功!", 0 + 48, "温馨提示!"
Dim C, D As String, E As Double
Open App.Path & "\2.txt" For Input As #2
If Dir(App.Path & "\2.txt") = "" Then
MsgBox "未找到正确文件", 0, "提示"
End If
Do While Not EOF(2)
Line Input #2, C
Do While InStr(C, " ") > 0 '循环直到检查到没有两个连续的空格
C = Replace(C, " ", " ")
Loop
Do While InStr(C, "<") > 0 '消除多余的<
C = Replace(C, "<", vbNullString)
Loop
D = D & C & vbCrLf
Loop
If D <> "" Then Print #2, D
Close
Close
Dim Filetxt As String
Dim Linetxt As String
Filetxt = ""
Open App.Path & "\2.txt" For Input As #2 '打开文件读。
Do While Not EOF(2) '循环至文件尾。
Input #2, Linetxt '将数据读入变量。
If Linetxt <> "" Then '如果非空行,就保存到变量
Filetxt = Filetxt & Linetxt & vbCrLf
End If
Loop
Close #2
Open App.Path & "\2.txt" For Output As #2 '打开文件写
Print #2, Filetxt
Close #2
End Sub