Reading a file bottom up in .Net 1.1

Posted on 2009-02-12
Last Modified: 2012-05-06
I have a large file (Its the IIS Log file) and the size is a good 50 MB. I have to write an app that can check the last 10 lines in the file (Bottom up approach) in VB.Net. Simple question, hopefully the solution is simple too.

I know we can read it top to bottom using a Stream Reader and doing a Read Line, I need a bottom up approach.

Remember, the line numbers are not known, the length of characters in each line are not known.

Question by:sujeshva
    LVL 23

    Expert Comment

    by:Jens Fiederer
    You can use a FileStream and use Seek to position yourself.  Start say 1000 characters to the end, read a bit more if the lines don't fit in.
    LVL 3

    Author Comment

    That would be too much trial and error in the code and a definite hit and miss. Moreover, the very first thing I have to check after reading a line is the date and time, which if I can read the line, I would be able to get by splitting the line using a space character and getting the first and second index in the array. so a random 1000 characters from the end will not be suitable.

    Already tried that and have given up.

    LVL 23

    Accepted Solution

    Given your formulation of the problem, there is NO way to guarantee you do not need to read the whole file.

    The simplest thing is to read the whole file and discard everything but the last 10 lines.  I figured you had thought of this already, and wanted something that would be more efficient in the case that you did NOT have to read the whole file.  

    This is the best you can do, you can NOT assume magic hardware that is able to read the bits backwards!

    Similar to splitting the lines by spaces, you can split what you read by newline character.  If you did not pick up at least 10 lines in your first read, read some more (prepend the new string to your buffer and split again).

    It is important or read a little MORE than 10 lines, because if you did not read at least the end-of-line character of the 11th line from the end you do not know you have it all.

    There is no trial and error involved here - there is just possibly more than a single read.
    LVL 3

    Author Comment

    You said it... you actually lit a bulb in my head and now I have a solution playing up based on what you just said... Oh I have been stupid, it was right there... thanks again, I will post my solution if it works the way I think it should.

    You are right, no trial and error, I am going to read more than 10 lines, probably 4096 chars, split em up based on the line feed, discard the first line and do the processing I have to do and then append the length of the discarded line to the position and read again. GREAT! now to make it work.


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now