Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I'm trying to find out how to read a text file backwards.

 

I want like a ReadLastLine() method of some kind, and then be able to step though the lines backwards.

 

I have some very large logfiles(2GB+) that I need to parse over time when new lines are added to those files.

 

Anyone got any ideas ?

 

Thanks

  • *Experts*
Posted

This is off the top of my head, but I would guess there's nothing to read a whole line from the end of file. You might be able to use a Seek type of method and manually find the EOL character to split the lines manually.

 

Sorry, not much help on this one. :)

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks for replying anyways Nerseus :)

 

I've used the seek method to put the pointer at EOF and then step back 2 bytes with offset -2, that gets me to the last character of the last line with text(the very last is a crlf).

 

Then i tried something like this:

 

dim buffer(1) as char

dim temp as string

 

do while not buffer(0) = vbLf

oStream.Read(buffer,0,1)

temp = temp & buffer(o).ToString

oStream.BaseStream.Position = oStream.BaseStream.Position - 2

loop

 

but this gives me some very strange results, almost like the cursor random "jumps" to different positions ??!

 

I'm puzzled :confused:

 

Any ideas will greatly appreciated, thanks :)

  • 4 weeks later...
Posted

Have you found the solution?

 

Hi Ronzan,

 

I am also trying to read text file backward...

I need to read a log file backwards, using activeX script in DTS.

But still unable to find the solution.

 

Have you solve this problem?

 

Anyone have any idea?

 

Regards,

Peipei:confused:

Posted

Yes I found a way to do it.

Sorry for not posting it before..

 

   Private Function GetNextLineReversed(ByVal lngStartPos As Long) As String
       '************************************************************************'
       '** GET NEXT LINE REVERSED                                             **'
       '** ------------------------------------------------------------------ **'
       '** Returns a text line backwards from a file, starting at lngStartPos **'
       '** Returns "eof" if no lines found                                    **'
       '************************************************************************'
       Try
           Dim myReader As New System.IO.StreamReader(myFileStream, System.Text.Encoding.Default)
           Dim temp As String = ""
           Dim sTempLine As String = ""
           Dim TempBlock(0) As Char
           Dim cBuffer(0) As Byte
           Dim lngLastLineStartPos As Long
           Dim lngLastLineEndPos As Long
           'Goto lngStartPos, step back to previous line
           'Save     for end of line
           lngLastLineEndPos = lngStartPos - 2

           If lngLastLineEndPos < 1 Then 'no can do, this would get us to     -1 in next statement
               Return "eof"
           End If

           'Loop to find first vbLF reversed
           myFileStream.Position = lngLastLineEndPos - 1
           myFileStream.Read(cBuffer, 0, 1)
           Do While Not ChrW(cBuffer(0)) = vbLf And myFileStream.Position > 1
               temp = temp & ChrW(cBuffer(0))
               myFileStream.Position = myFileStream.Position - 2
               myFileStream.Read(cBuffer, 0, 1)
           Loop
           'Save     for start of line
           lngLastLineStartPos = myFileStream.Position

           'Clear variables
           TempBlock = ""
           sTempLine = ""

           'Get the line from lngLastLineEndPos to lngLastLineStartPos
           ReDim TempBlock(lngLastLineEndPos - lngLastLineStartPos)
           myFileStream.Position = lngLastLineStartPos
           myReader.ReadBlock(TempBlock, 0, lngLastLineEndPos - lngLastLineStartPos)
           sTempLine = TempBlock

           'here you can save the start position of the last line parsed
'save it in a global variable to use it as a new start posistion when calling the GetNextLineReversed() function again
           'g_lngLastLineStartPosition = lngLastLineStartPos

           'return the line
           Return sTempLine

       Catch
           'insert error handling here
       End Try

End Function


'To use the function:
'----------------------------------------------------
Dim strFileName as string
Dim myFileStream as FileStream
Dim lngStartPos as long

'file name and path to parse
strFileName = "c:\test.txt"

'Create a global filestream object
myFileStream = New System.IO.FileStream(strFileName,System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)

'go to end of file and save position (stream.length can also be used to find the last byte    )
myFileStream.Seek(0, SeekOrigin.End)
lngStartPos = myFileStream.Position

sLine = GetNextLineReversed(lngStartPos)

'----------------------------------------------------

 

Im sure there is a better/easier way to do this, but the was what I could come up with for the time being due to a deadline for the project where I use this function :)

 

Let me know if you have any questions regarding the function.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...