Jaymond Flurrie
Well-known Member
- Joined
- Sep 22, 2008
- Messages
- 921
- Office Version
- 365
- Platform
- Windows
So the situation is that I have a text file with a million lines. I need to read a certain line from there. How do I do it so that I don't read every single line from the beginning, but instead, say, line #431278 without reading the lines 1 to 432177 first?
The idea is that since the file is more than huge (over 4 GB), I need to avoid the massive delay that I expect to get if I read the lines in order like 1-1-2-1-2-3-1-2-3-4-1-2-3-4-5 (to get the five first ones) and so on would cause when I need to take a line, process it, pass it forward and take a new line.
Here's a code I use to read lines right now:
but that has the problem that when I get to any later lines, it most likely shoots the processing time thru the roof.
The idea is that since the file is more than huge (over 4 GB), I need to avoid the massive delay that I expect to get if I read the lines in order like 1-1-2-1-2-3-1-2-3-4-1-2-3-4-5 (to get the five first ones) and so on would cause when I need to take a line, process it, pass it forward and take a new line.
Here's a code I use to read lines right now:
Code:
sFileName = "C:\textfile.txt"
If Len(Dir$(sFileName)) = 0 Then
Exit Function
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, sBuf
' now you have the next line of the file in sBuf
' do something useful:
vLine = Split(sBuf, Chr(9)) 'Split on tabs
Loop
' close the file
Close iFileNum
but that has the problem that when I get to any later lines, it most likely shoots the processing time thru the roof.