我需要读取一个可能很大的文本文件的最后一行,而不加载整个文件。我想我有一个很好的解决方案,但有一个令人困惑的错误。对于大多数条目,该函数正确返回,但在一种特定情况下,它似乎将文件的最后一个字母加倍。这是我编写的测试脚本(news.txt 的内容是单个单词“NEWS”):
<?php
echo "file contains: ".file_get_contents('news.txt')."<br/>";
$lastLine = getLastLine("news.txt");
echo "function returns: ".$lastLine;
function getLastLine($filename){
$f = fopen($filename, 'r');
$cursor = -1;
$line = '';
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
echo "initial read: <br/>"; // DEBUG
echo $cursor." | ".$char." | ".$line."<br/>"; // DEBUG
// test for empty file
if($char === false)
return false;
// Trim trailing newline chars of the file
while ($char === "\n" || $char === "\r" || $char === " ") {
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
echo "kill new lines: <br/>"; // DEBUG
echo $cursor." | ".$char." | ".$line."<br/>"; // DEBUG
// Read until the start of file or first newline char
echo "running loop: <br/>"; // DEBUG
while ($char !== false && $char !== "\n" && $char !== "\r" ) {
// Prepend the new char
$line = $char . $line;
echo $cursor." | ".$char." | ".$line."<br/>"; // DEBUG
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
return trim($line);
}
?>
输出如下所示:
file contains: NEWS
initial read:
-1 | S |
kill new lines:
-1 | S |
running loop:
-1 | S | S
-2 | S | SS
-3 | W | WSS
-4 | E | EWSS
-5 | N | NEWSS
function returns: NEWSS
似乎索引 -1 和 -2 是相同的,但是 file_get_contents 确认文件的内容只是“新闻”。
我究竟做错了什么?