Text file inputting: begin from the end or skipping lines?

G

Guest

Hi,

I'm importing a large text file to Access. I'd like to start reading the
file beginning from the end, as I normally need need only approx. the last
1000 lines (and reading the other lines is just time consuming).

Now I'm forced to read all the lines:

Open fileName For Input As FID
Line Input #FID, Buffer
While Not EOF(FID)
prosessLine (Buffer)
Wend

If it is not possible, it would also be helpful if I could read for example
every 100th line and, when information new enough is found, read all lines
from that on (and the 99 lines before that).

Every line contains over 1000 char (including the date, which I use as
indetificator), so reading all >10000 lines is really time burdening!

Thanks in advance,
Anssi K.
 
T

Tim Ferguson

Every line contains over 1000 char (including the date, which I use as
indetificator), so reading all >10000 lines is really time burdening!

You might find it handy to write an indexer for the file; otherwise if you
know exactly how long each line is, you could treat it as a binary file and
Seek straight into it.

You could use Seek to start, say, three quarters of the way through and the
read up to the nearest vbNewLine sequence. Your algorithm would need to be
able to back track if your starting point turned out to be too far on.

My guess would just be to read each line into a collection of strings, and
delete the 1st every time you get to 101.

All the best


Tim F
 
J

John Nurick

For really big files I'd shell out to some serious text-processing
tools, but with only 10,000 lines something like this *air code* should
work OK:

Dim j As Long, FilePos As Long
Dim InterestingLineFound As Boolean

Open FN For Input As FID
InterestingLineFound = False
Do Until InterestingLineFound = True Or EOF(FID)
j = 0
FilePos = Seek(FID)
Do Until j = 100 or EOF(FID)
Line Input #FID, Buffer
j = j + 1
Loop
'test contents of Buffer.
If we're in the area of interest Then
InterestingLineFound = True
End If
Loop

If EOF(FID) Then exit sub

'If we're here, we've found an interesting line
'but have probably overshot
Seek #FID, FilePos 'go back to our last bookmark

'Process the remaining lines
...
 
L

LanceMcGonigal

If the file is a structured file then you could link to it (like you do a
table in another database) and query it in reverse sequence. Just establish
the link and run the query as an input set to your code.

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top