We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Reading a file bottom up in .Net 1.1

sujeshva
sujeshva asked
on
Medium Priority
554 Views
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.

Thanks,
Suj
Comment
Watch Question

Jens FiedererTest Developer/Validator

Commented:
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

Commented:
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.

-Suj
Test Developer/Validator
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.

-Suj
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.