1

I currently have a text file that has fixed fields. There are x number of header lines and x number of detail lines. There is one piece of information I need from the detail line so I can create a record for my program. If there is a way I can loop from the end of the file to the beginning of the file I will be able to accomplish my task.

I have the code below that does the beginning of file to the end...

    Using rdr As New StreamReader(_mtLocation)
        Do Until rdr.EndOfStream
           '// Do code here

        Loop
    End Using

Is there a way to go from the end of file to the beginning?

If there is any other information you need, please let me know and I will update the question with additional information. Any help is appreciated.

John Janssen
  • 293
  • 2
  • 12
  • 30
  • Do you mean you know the size of the fields? – Ry- Jul 28 '14 at 17:26
  • Yes the company gave us the fixed lengths of each of the fields. – John Janssen Jul 28 '14 at 17:27
  • Read all lines into an array and then loop through the array backwards. – The Blue Dog Jul 28 '14 at 17:27
  • You can probably `Seek` straight to the appropriate location, then. Could you give an example of the format? – Ry- Jul 28 '14 at 17:27
  • @TheBlueDog I was thinking of doing that but some of these text files can have hundreds/ thousands of lines. I feel this would have a performance impact. – John Janssen Jul 28 '14 at 17:30
  • I would try it and then worry about tuning up the performance if and when you establish that it's a problem. Premature optimization, you know... – Douglas Barbin Jul 28 '14 at 17:32
  • @JohnJanssen: Hundreds/thousands/tens of thousands of lines is not that much of a big deal. Of course, if you are talking about gigabyte file sizes then that's a different matter... – The Blue Dog Jul 28 '14 at 17:34
  • You can't really search a file from end to beginning. (Well, it would be very slow. You'd have to seek to the end of the file - the sizeof one record, read a record of the file, and see if it matches. If it doesn't you're at the end of the file again, and you seek back 2 records and read 1, and see if it matches. If it doesn't, you seek back 2 more records (the one you just read and the one before it), read 1 record, and repeat until you either find the record or can't seek back any further.) – Ken White Jul 28 '14 at 17:37
  • It's in C# but it could help you out http://stackoverflow.com/a/452945/130611 – the_lotus Jul 28 '14 at 18:09

3 Answers3

1

You can read the file into an array of String using the File.ReadAllLines method. Then you would iterate backwards through that array, which would give you each line from bottom to top. For each line, you would iterate backwards through the line.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
1

You can read at different position of the stream by changing the Position property. The problem happens depending on the encoding of the file.

A had a simple file (not unicode) with the number 1 through 9 written into it.

    Using s = System.IO.File.OpenRead("test.txt")
        For i As Integer = 8 To 0 Step -1
            s.Position = i
            Console.WriteLine(Chr(s.ReadByte()))
        Next
    End Using

There's a nice example here but in C#. stackoverflow.com/a/452945/130611

the_lotus
  • 12,668
  • 3
  • 36
  • 53
0

This is another solution:

Dim TxtLines As New List(Of String)
Using rdr As New StreamReader(_mtLocation)
    Do Until rdr.EndOfStream
        TxtLines.Add(Reader.ReadLine.ToString())
    Loop
End Using

Dim x As Integer
For x = TxtLines.Count To 0 Step -1
    'Do your code here...
Next

To get the text of the current line just do this inside the For loop:

TxtLines(x).ToString()
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75