[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Reading a file bottom up in .Net 1.1

Posted on 2009-02-12
Medium Priority
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
  • 2
  • 2
LVL 23

Expert Comment

by:Jens Fiederer
ID: 23624885
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.

Author Comment

ID: 23624942
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

Jens Fiederer earned 375 total points
ID: 23625086
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.

Author Comment

ID: 23625364
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

834 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