note 87949 modified in function.fscanf by danbrown
| From: | danbrown@php.net | Date: | Thu, 01 Jan 2009 20:09:37 +0000 |
| Subject: | note 87949 modified in function.fscanf by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148747@lists.php.net to get a copy of this message | ||
The fgetd() function suggested by worldwideroach on 14-Jul-2005 04:33 does not handle the last
buffer-load of data correctly. At least not for my requirement. It is possible for EOF to have been
reached but for there still to be characters in $sRecord.
For the, er... record I got better results with this version:
<?php
function fgetd(&$rFile, $sDelim, $iBuffer=1024) {
$sRecord = '';
while(!feof($rFile)) {
$iPos = strpos($sRecord, $sDelim);
if ($iPos === FALSE) {
$sRecord .= fread($rFile, $iBuffer);
} else {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
}
// Last read got some more data before hitting EOF?
if ($sRecord != '') {
if (($iPos = strpos($sRecord, $sDelim)) !== FALSE) {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
else {
return $sRecord;
}
}
else {
return FALSE;
}
}
?>
--was--
The fgetd() function suggested by worldwideroach on 14-Jul-2005 04:33 does not handle the last
buffer-load of data correctly. At least not for my requirement. It is possible for EOF to have been
reached but for there still to be characters in $sRecord.
For the, er... record I got better results with this version:
function fgetd(&$rFile, $sDelim, $iBuffer=1024) {
$sRecord = '';
while(!feof($rFile)) {
$iPos = strpos($sRecord, $sDelim);
if ($iPos === FALSE) {
$sRecord .= fread($rFile, $iBuffer);
} else {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
}
// Last read got some more data before hitting EOF?
if ($sRecord != '') {
if (($iPos = strpos($sRecord, $sDelim)) !== FALSE) {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
else {
return $sRecord;
}
}
else {
return FALSE;
}
}
http://php.net/manual/en/function.fscanf.php