2017-01-27 180 views
0

我現在在刪除文本文件中的行時出現問題,刪除了文本文件中的一行字符串後,在其上留有空白區域。這是我的代碼。感謝您幫助我們。在刪除文本文件中的行後刪除空白VB6

iFileList = FreeFile 
iFileList2 = FreeFile 

Open App.Path & "\months\" & gMonth & ".txt" For Input As #iFileList 

Do While Not EOF(iFileList) 
    Line Input #iFileList, sLine 
    tempHolder2 = Split(sLine, "/") 
    If Len(sLine) > 0 Then 
     If gDay = tempHolder2(0) Then 
      If tempHolder2(1) Like lvAlarm.selectedItem Then 
       'skip the line 
      Else 
       sNewText = sNewText & sLine & vbCrLf 
      End If 
     End If 
    End If 

Loop 
Close 
Debug.Print (sNewText) 
iFile = FreeFile 
Open App.Path & "\months\" & gMonth & ".txt" For Output As #iFile 
'sNewText = sNewText & vbCrLf 
Print #iFile, Trim(sNewText) 
Close 

回答

2

我猜你所指的空白處在文件末尾,而不是散佈在其他行內的某處。如果是這種情況,我懷疑這是因爲在將sNewText變量的內容打印到文件時使用的Trim命令不會刪除字符串末尾的最後一個回車符/換行符對。

刪除尾隨換行符,你應該做到以下幾點:

If Right$(sNewText,2) = vbCrLf Then 
    sNewText = Left$(sNewText, Len(sNewText) - 2) 
End If