Showing posts with label computing. Show all posts
Showing posts with label computing. Show all posts

Thursday, October 29, 2009

read the last line of a file in PHP

If your not a gh33k just move along.
move along, this is not the post you are looking for.

Had a hard to track down bug today where some code that was supposed to return the last line of a log file failed - the logs had recently been rotated and the new file had no lines in it yet. I modified it to create a pointer to the beginning of the file and break out of the infinite loop if the pointer cursor ever gets back to the beginning of the file. With a few minor changes it will work in C C++ C# or Java as they all have something similar to fseek, ftell fclose etc



/**
*
* Reads the last line of a, presumably large, logfile
* without loading the whole file into memory
* Safe to use for 0 or 1 line files
*
*
* @param string
* @return string
*
*/
function readlastline($fileName)
{

$fp = @fopen($fileName, "r");
$begining = fseek($fp, 0);
$pos = -1;
$t = " ";
while ($t != "\n") {
fseek($fp, $pos, SEEK_END);
if(ftell($fp) == $begining){
break;
}
$t = fgetc($fp);
$pos = $pos - 1;
}
$t = fgets($fp);
fclose($fp);
return $t;
}

Wednesday, September 10, 2008

Speed

I've just been getting outrageously high download speeds on the cable at home.
Averaging 336 kB/s

Hypothetically, if one were to say download the first episode of the second series of *cough* Terminator on something like bittorrent at 350 MB it would take just 17 minutes to download.

Wednesday, July 23, 2008

Access Denied

If you suddenly can't access your USB memory stick, camera, phone or other mass storage device, in my case the 500Gb external hard drive, and you are using an Acer machine then for the love of [pick a god] Go into into Control Panel >> Add or Remove Programs >> remove everything Acer especially the LanScope and then reboot.
No more dreaded "Administrator has prohibited access to USB/1394 mass storage devices"

Thursday, May 08, 2008

How am I looking - cool, stylish?

I should hope so, because this is being posted from my new Macbook Pro - the big one, wide wide screen, zillions of gigglebytes per nano second or whatever.

off to watch a movie on it - bye