2014-11-22 57 views
0

當我將600KB文本文件(HTML代碼)讀入字符串變量時,它截斷了大約一半的內容。這裏是我有的代碼...我哪裏錯了?VB6字符串變量截斷從文件中讀取的數據

Dim fso As New FileSystemObject 
Dim f As File 
Dim fsoStream As TextStream 
Dim strLine As String 
Set f = fso.GetFile("C:\Users\Neanderthal\Desktop\MyProj\GMATClubLog.txt") 
Set fsoStream = f.OpenAsTextStream(ForReading) 
' Read the file line by line, printing the results to the Form 
Do While Not fsoStream.AtEndOfStream 
    strLine = fsoStream.ReadLine 
    Debug.Print strLine 
Loop 
Len(strLine) 

fsoStream.Close 
Set fsoStream = Nothing 
Set f = Nothing 
Set fso = Nothing 

基本上我爲什麼要閱讀文本文件的全部內容,是因爲我想提取重複一套基於搜索條件的數據。這是重複碼

<td class="topicsName" style="width:100%"> 
<a class="newestPostIcon" href="http:someURL.com"></a> 
<a title="some text" href="http://I want to extract this link.html" ></a> 
</td> 
+0

您正在將每行讀入字符串變量。你是說每一行都被截斷了,或者只是通過文件的一半 – Rob 2014-11-23 10:32:08

+0

它停止在文件中的特定字符嗎?它總是停止相同的位置? – Hrqls 2014-11-24 13:25:39

回答

0

在整個文件轉換成字符串讀取最簡單的方法是:

Dim intFile As Integer 
Dim strFile As String 
Dim strData As String 
strFile = "c:\temp\file.txt" 
intFile = FreeFile 
Open strFile For Input As #intFile 
    strData = Input(LOF(intFile), #intFile) 
Close #intFile 

如果你想,那麼你可以將數據分割成線的陣列如下:

Dim strLine() As String 
strLine = Split(strData, vbCrLf) 

然後循環通過陣列(例如以打印表單上的每個行):

Dim lngIndex As Long 
For lngIndex = 0 To UBound(strLine) 
    Print strLine(lngIndex) 
Next lngIndex