注册 登录
编程论坛 VB6论坛

请教,txt文件处理后内容格式变化的问题。

ictest 发布于 2017-04-07 12:45, 1241 次点击
比如一个文本里面格式如下(注意每行前面都有空格):
@123
    -1
    -5
    -10
@234
    -2
    -4
@345
    -3
    -8
经程序处理后(程序的目的是我输入一个数字,如果数字为@后面的字符串就删除@那行和下个@之前的那些行。比如我输入“234”,理想的输出文本内容应为:
@123
    -1
    -5
    -10
@345
    -3
    -8
但实际输出文本的内容如下,每行前面的空格没有了:
@123
-1
-5
-10
@345
-3
-8
请问程序中哪里需要修改?
附程序:
在窗体中添加下列代码并画上按钮控件command1和text1
程序代码:


Private Sub Command1_Click()
Dim FileRead As Integer
Dim FileWrite As Integer
Dim Str As String
Dim flag1, flag2 As Boolean
flag1 = False
flag2 = False
FileRead = FreeFile
If Dir(App.Path & "\" & "testfile.txt") <> "" Then
Open App.Path & "\" & "testfile.txt" For Input As FileRead

FileWrite = FreeFile
Open App.Path & "\" & "Tmp.txt" For Output As FileWrite
Do While Not EOF(FileRead)
If flag1 = False Then
Input #FileRead, Str
If InStr(Str, "@" & Text1.Text) <> 0 Then
flag1 = True
flag2 = False
Else
Print #FileWrite, Str
End If
Else
Input #FileRead, Str
If InStr(Str, "@") <> 0 And InStr(Str, "@" & Text1.Text) = 0 Then
flag2 = True
ElseIf InStr(Str, "@" & Text1.Text) <> 0 Then
flag1 = True
flag2 = False
End If
If flag2 = True Then
Print #FileWrite, Str
End If
End If
Loop
Close FileRead
Close FileWrite
Kill App.Path & "\" & "testfile.txt"
Name App.Path & "\" & "Tmp.txt" As App.Path & "\" & "testfile.txt"
Else
MsgBox "文件不存在!"
End If
End Sub


[此贴子已经被作者于2017-4-7 12:47编辑过]

2 回复
#2
xzlxzlxzl2017-04-07 20:44
Input #FileRead, Str 改成 Line Input #FileRead, Str即可
#3
ZHRXJR2017-04-08 03:24
同意2楼的观点,改成 Line Input #FileRead, Stt就可以了。
那么 Input  和 Line Input  的区别是什么
 Line Input  是整行读取数据,遇到行结束的回车符才读下一行数据
 Input  是在遇到空格、符号(包括。号,号、号等等)、回车符就读取一个数据,例如如果文件中一行中这样
“  123  345,567、789  876”,读出的是5个数据“123345567789876”,注意是5个数据的组合,没有了空格、符号。
一行数据就读出5个数据,那么一行数据多的话,就是读出多个数据。
1