PHP的feof()方法需要注意的地方
2011-11-11 08:53
399 查看
简介:这是PHP的feof()方法需要注意的地方的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340516' scrolling='no'>自己曾写过这样一段代码:
其中当读取行数包括文件结尾的时候,$result数组中总会比期望的内容多出来一个元素:
按说,如果读取到最后一行,feof函数会返回TRUE,然后while循环就退出了,为什么不是呢?
事情原来是这样子的:
feof() is, in fact, reliable.
However, you have to use it carefully in conjunction with fgets(). A common
(but incorrect) approach is to try something like this:
The problem when processing plain text files is that
feof() will not return true after getting the last line of input. You
need to try to get input _and fail_ before feof() returns true. You can
think of the loop above working like this:
* (merrily looping, getting lines and processing
them)
* fgets used to get 2nd to last line
* line is processed
* loop back up -- feof returns false, so do the
steps inside the loop
* fgets used to get last line
* line is processed
* loop back up -- since the last call to fgets
worked (you got the last line), feof still returns false, so you do the steps
inside the loop again
* fgets used to try to get another line (but
there's nothing there!)
* your code doesn't realize this, and tries to
process this non-existent line (typically by doing the same actions again)
* now when your code loops back up, feof returns
true, and your loop ends
There's two ways to solve this:
1. You can put an additional test for feof()
inside the loop
2. You can move around your calls to fgets() so
that the testing of feof() happens in a better location
Here's solution 1:
And here's solution 2 (IMHO, more elegant):
FYI, the eof() function in C++ works the exact
same way, so this isn't just some weird PHP thing...
Ref: http://cn.php.net/manual/en/function.feof.php
爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/340516.html pageNo:7
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340516' scrolling='no'>自己曾写过这样一段代码:
if(file_exists($pmr_config["datasetfile"])){ $tmp_counter = 0; $hd = fopen($pmr_config["datasetfile"], "r"); if($hd !== FALSE){ while (!feof($hd)) { $buffer = fgets($hd); if($tmp_counter >= $seq){ $result[] = $buffer; } $tmp_counter++; if($tmp_counter >=$seq + $size){ break; } } }else{ echo "warning:open file {$pmr_config["datasetfile"]} failed!PHP_EOL"; } }else{ echo "warning:file {$pmr_config["datasetfile"]} does not exsits!PHP_EOL"; }
其中当读取行数包括文件结尾的时候,$result数组中总会比期望的内容多出来一个元素:
(boolean)false
按说,如果读取到最后一行,feof函数会返回TRUE,然后while循环就退出了,为什么不是呢?
while (!feof($hd)) {
事情原来是这样子的:
<?php // if file can not be read or doesn't exist fopen function returns FALSE $file = @fopen("no_such_file", "r"); // FALSE from fopen will issue warning and result in infinite loop here while (!feof($file)) { } fclose($file); ?>
feof() is, in fact, reliable.
However, you have to use it carefully in conjunction with fgets(). A common
(but incorrect) approach is to try something like this:
<? $fp = fopen("myfile.txt", "r"); while (!feof($fp)) { $current_line = fgets($fp); // do stuff to the current line here } fclose($fp); ?>
The problem when processing plain text files is that
feof() will not return true after getting the last line of input. You
need to try to get input _and fail_ before feof() returns true. You can
think of the loop above working like this:
* (merrily looping, getting lines and processing
them)
* fgets used to get 2nd to last line
* line is processed
* loop back up -- feof returns false, so do the
steps inside the loop
* fgets used to get last line
* line is processed
* loop back up -- since the last call to fgets
worked (you got the last line), feof still returns false, so you do the steps
inside the loop again
* fgets used to try to get another line (but
there's nothing there!)
* your code doesn't realize this, and tries to
process this non-existent line (typically by doing the same actions again)
* now when your code loops back up, feof returns
true, and your loop ends
There's two ways to solve this:
1. You can put an additional test for feof()
inside the loop
2. You can move around your calls to fgets() so
that the testing of feof() happens in a better location
Here's solution 1:
<? $fp = fopen("myfile.txt", "r"); while(!feof($fp)) { $current_line = fgets($fp); if (!feof($fp)) { // process current line } } fclose($fp); ?>
And here's solution 2 (IMHO, more elegant):
<? $fp = fopen("myfile.txt", "r"); $current_line = fgets($fp); while (!feof($fp)) { // process current line $current_line = fgets($fp); } fclose($fp); ?>
FYI, the eof() function in C++ works the exact
same way, so this isn't just some weird PHP thing...
Ref: http://cn.php.net/manual/en/function.feof.php
爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/340516.html pageNo:7
相关文章推荐
- PHP的feof()方法需要注意的地方
- PHP在安全上需要注意的地方[翻译]
- java中需要注意的地方,包含方法传递参数等
- JQUERY中的getJSON方法需要注意的一个地方:单引号与双引号
- PHP常量使用的几个需要注意的地方(谨慎使用PHP中的常量)
- 我在安装TFS 2008的时候遇到的问题以及解决方法一安装TFS需要注意的地方
- C#获取程序路径的方法中需要注意的地方
- android之canvas.draw方法需要注意的地方
- PHP json_encode函数中需要注意的地方
- PHP中通过调用call_user_func_array函数执行mysqli_stmt_bind_param方法时需要注意的问题
- 提高PHP代码质量需要注意的地方一
- PHP IN_ARRAY 函数使用需要注意的地方
- php写mysql语句需要注意的一个小地方
- 选区的几种方法及需要注意的地方
- php 的继承中需要注意的地方
- 提高PHP代码质量需要注意的地方二
- C++中方法定义和调用需要注意的一个地方
- 使用setBounds()方法需要注意的地方
- php需要注意的地方
- PHP IN_ARRAY 函数 使用需要注意的地方