VB-ers,
I just discovered this site this morning and must say that I am excited by the find. This appears to be a great forum.
My question is this: I stumbled upon a seemingly great way to load a text file to an array in the FAQ. The code is:
Get #ff, 1, wholefile
Close #ff
If InStr(wholefile, vbCrLf) > 0 Then
endline = vbCrLf 'ASCII DOS format
Else
endline = vbLf 'ASCII UNIX format
End If
ReDim individ_lines(0)
Do
Cloc = InStr(wholefile, endline)
If Cloc > 0 Then 'Parse the data
ReDim Preserve individ_lines(0 To UBound(individ_lines) + 1)
tmp = Left(wholefile, Cloc - 1)
individ_lines(UBound(individ_lines)) = tmp
Else
Exit Do 'All data has been parsed
End If
wholefile = Right(wholefile, Len(wholefile) - (Len(tmp) + Len(endline)))
Loop
This would seem to be a great and quick way to parse the file. Why is it so much slower than my sloppy method below?
Do While (Not EOF(1))
inchar = ""
readline = ""
Do While (Not EOF(1)) And (inchar <> vbLf)
readline = readline + inchar
inchar = Input(1, #1)
Loop
If Len(readline) > 0 Then
'I parse the line of text here
End If
readline = ""
Loop
As you can see, I parse the file character by character. I would think the second method would be much slower, but it is actually faster. Any thoughts? (I use VB 5.)
Ryan
I just discovered this site this morning and must say that I am excited by the find. This appears to be a great forum.
My question is this: I stumbled upon a seemingly great way to load a text file to an array in the FAQ. The code is:
Get #ff, 1, wholefile
Close #ff
If InStr(wholefile, vbCrLf) > 0 Then
endline = vbCrLf 'ASCII DOS format
Else
endline = vbLf 'ASCII UNIX format
End If
ReDim individ_lines(0)
Do
Cloc = InStr(wholefile, endline)
If Cloc > 0 Then 'Parse the data
ReDim Preserve individ_lines(0 To UBound(individ_lines) + 1)
tmp = Left(wholefile, Cloc - 1)
individ_lines(UBound(individ_lines)) = tmp
Else
Exit Do 'All data has been parsed
End If
wholefile = Right(wholefile, Len(wholefile) - (Len(tmp) + Len(endline)))
Loop
This would seem to be a great and quick way to parse the file. Why is it so much slower than my sloppy method below?
Do While (Not EOF(1))
inchar = ""
readline = ""
Do While (Not EOF(1)) And (inchar <> vbLf)
readline = readline + inchar
inchar = Input(1, #1)
Loop
If Len(readline) > 0 Then
'I parse the line of text here
End If
readline = ""
Loop
As you can see, I parse the file character by character. I would think the second method would be much slower, but it is actually faster. Any thoughts? (I use VB 5.)
Ryan