Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a text file from bottom to top

Status
Not open for further replies.

bakerm

Programmer
Apr 25, 2000
53
0
0
US
I need to read a text file from the last line up. I have no problem itterating through a file from beginning to end, but this has provided some puzzlement for me.

Thanks in advance for your help!
 
Hi,

I don't think you can do this working with the file as a Text file, you'd have to read it as a Binary file, and use CRLF to figure out where each line starts.

- Andy.
 
Just load the file into a single string var, and use SPLIT to seperate into an array of lines. Read then (the array of lines) in any order you choose.

It is also much faster than most other methods.

Code:
Public Function basNewArray(FilIn As String) As Variant

    Dim FilId As Integer
    Dim Idx As Integer
    Dim Jdx As Integer
    Dim Kdx As Integer
    Dim RawFile As String
    Dim MyLines As Variant
    Dim MyWords As Variant
    Dim TempArray(100, 7) As Variant
    Dim Mu() As Variant

    'I 've an array mu(MAX_1,MAX_2) where MAX_1=100,MAX_2=7

    'However once I've assigned calculations to it it may turn out that
    'the first index is 2 and the second is 5.

    'How do I redim preserve such an array?
    'If I leave it it just prints out about 700 zeros, and I know that
    'you can only redim preserve the second index in a 2-dim array.


    'SAMPLE Usage:
    '? basNewArray("C:\My Documents\Fleas.Txt")

    'C:\My Documents\Fleas.Txt is listed below
    'My Dog Has Fleas
    'Your Dog Has Fleas
    'His Dog Has Fleas
    'All Dogs Have Fleas
    'This Cat Has Fleas
    'That Cat Has Fleas
    'Those Cats Have Fleas
    'Who Doesn 't Have Fleas
    'I Don 't Have Fleas


    FilId = FreeFile                    'File Handle
    Open FilIn For Binary As FilId      'Open File

    RawFile = Space(LOF(FilId))         'Assign File String proper Length
    Get #FilId, 1, RawFile              'Get File into String

    MyLines = Split(RawFile, vbCrLf)    'Split file into an Array of Strings


'Do your other stuff (reading?) here, or make the array global and just fill it here, or set the calling prcedure up to expect an array as the return value and just use this to get the file into "lines".

There is SOME extra stuff (esp declarations) as I just copied ~~~~ 1/3 of another routine.
MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
...
dim intFileHandle as integer
dim strFileName as string
dim strInRec(0) as string
...
strFileName = "c:\temp\InFile.txt"
...
open strFileName For Input AS #intFileHandle
While not EOF(intFileHandle)
line input #intFileHandle, strInRec(ubound(strInRec))
redim preserve strInRec(ubound(strInRec) + 1)
Wend
close #intFileHandle
...
For i = ubound(strInRec)-1 to 0 step -1

...
process_record_here
...
next i
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top