Ok, so I need to read this file from the EOF to SOF...I was going to write up a class but found one by Steve Weet <sweet at weet dot demon dot co dot uk>.
So I am using/editing this class right now, and have an issue. I am unable to read the last line of this file I have...I think the issue is that there is no new line at the end of the file, so the last line is skipped...
Here is the revFile class:
class revFile{
var $FileName;
var $FileHandle;
var $FilePos;
function revFile($filename) {
$this->FileName = $filename;
$this->FileHandle = @fopen($filename, "r") or die("Could not open file ". $this->filename ."\n");
// Find EOF
if (!(fseek($this->FileHandle, 0, SEEK_END) == 0))
die ("Could not find end of file in ". $this->filename ."\n");
// Store file position
$this->FilePos = ftell($this->FileHandle);
// Check that file is not empty or doesn;t contain a single newline
if ($this->FilePos < 2 )
die ("File is empty\n");
// Position file pointer just before final newline
// i.e. Skip EOF
$this->FilePos -= 1;
}
function GetLine(){
$pos = $this->FilePos -1;
$ch = "";
$line = "";
while($ch != "\n" && $pos >= 0){
fseek($this->FileHandle, $pos);
$ch = fgetc($this->FileHandle);
// Decrement out pointer and prepend to the line
// if we have not hit the new line
if($ch != "\n" && $ch != "\r") {
$pos = $pos -1;
$line = $ch . $line;
}
}
$this->FilePos = $pos ;
return $line . "\n";
}
function sof() {
return ($this->FilePos <= 0 );
}
}
And here is my code for finding a specific line:
function parseLog(){
$reverseFile = new revFile($this->logFileDir.self::LOGFILE);
while (!$reverseFile->sof()) {
if (preg_match('/^\[([0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2})\]/', $reverseFile->GetLine(), $matches)) {
$this->status = $matches[1];
break;
}
}
And here is the data from the file Im reading:
[21:13:43] Some event here
[21:28:51] Another event here
[21:53:08] Blah blah blah
[21:46:52] Some more stuff here
Now, the last line "[21:46:52] Some more stuff here" is the very last line in the file. Even if I give the file a return (just hit the Enter key once), my script still brings back "21:53:08" (without quotes). I need it to bring back 21:46:52 instead.
If I hit Return 2xs then the script works...only problem is, I am reading a log file that another program outputs, so unless I automatically write \n to the end first, and then do my script, I am stuck (and I dont want to do this).