users.txt
Ale ale@example.com
Nicole nicole@example.com
Laura laura@example.com
1.识别文件末尾字符
程序需要一种标准的方式来识别何时到达文件的末尾。。这个标准通常称为文件末尾(或EOF)字符。
在PHP中,此函数是feof()。feof()函数用来确定是否到达资源末尾,它在文件I/O操作中经常使用。
<?php
$fh = fopen("data/users.txt","r");
//当未达到EOF时,检索下一行
while(!feof($fh)){
echo fgets($fh);
}
//关闭文件
fclose($fh);
?>
Ale ale@example.com Nicole nicole@example.com Laura laura@example.com
2.读取指定数目的字符
fgets()函数返回通过打开的资源句柄读入的若干个字符,或者返回遇到换行或EOF之前读取的所有内容
string fgets(resource handle [,int length])
如果忽略可选的length参数,则假设为1024个字符。在大多数情况下,这意味着fgets()将在读取到1024个字符前遇到换行符,因而每次成功调用都会返回下一行
<?php
$fh = fopen("data/users.txt","r");
echo fgets($fh);
//关闭文件
fclose($fh);
?>
Ale ale@example.com
此例子比上例缺少while循环,fget()遇到换行符就停止读取了。

被折叠的 条评论
为什么被折叠?



