I also want to make it so that the algorithm is better able to tune itself if we grossly underestimate what $linelength should be. Typically, I want it to be able to read ‘n’ lines forward from the offset given, however if that proves insufficient, I’m going to grow the offset by 10% and try again. My approach to this is to provide an approximation to the average size of a line: $linelengthĪnd we have the byte $offset from the end of the file that we will seek to to begin reading.Ī check we need to do right here is that we haven’t offset to before the beginning of the file, and so we have to override $offset to match the file size.Īlso as I’m being over-cautious, I’m going to tell it to offset $linecount + 1 – The main reason for this is by seeking to a specific byte location in the file, we would have to be very lucky to land on the first character of a new line – therefore we must perform a fgets() and throw away that result. If insufficient lines are returned, it incrementally looks back further in the file until it either can look no further, or sufficient lines are returned.Īssumptions need to be made in order to tune the algorithm for the competing challenges of :
However, here I will demonstrate a fairly tuned method of seeking to the end of the file and stepping back to read sufficient lines to the end of the file.
There are several approaches one can take to solving the problem (as always in programming there are many ways to skin the proverbial cat) – taking the easy way out and shelling out to run the tail command or reading the file line by line keeping the last ‘n’ lines in memory with a queue.
Just another bit of a disclaimer: Namely so I don't get some person writing and saying 'why are you writing this' its because this is a general discussion and everyday chit chat thread and I can't be the only person on the planet who might enjoy learning such stuff.I read Kevin’s Read Line from File article today and thought I would add some sample code I wrote a while back to address a similar task in PHP – How to perform the equivalent of “tail -# filename” in PHP.Ī typical task in some applications such as a shoutbox or a simple log file viewer is how to extract the last ‘n’ lines from a (very) large file – efficiently.
Like calculating how many seconds it takes for the light of the moon to reach the earth (its 1.28 seconds) in case you were wondering. It's a slick little program that so far I've learned how to do some simple commands which will do math for me! I find that I spend a lot of time on non-instructional things (blabbing here mostly) and I was thinking instead of yapping, maybe I could learn something which takes up very little time and is free. Since I started a temp job, and we were experiencing some issues with excel, I figured that maybe finding out more about programming might be useful in the future.
I seen it offered free though (a 9 week program) which only takes up a few minutes (literally many videos are broken down into 5-20 minute segments) and I can cover two weeks in about 2 hours. So for a few years I've noticed there was a program called Matlab which was used in data analysis, but I always kind of shied away from it because well its programming.