PHP中的fgetc()函数是一个内置函数,用于从打开的文件中返回单个字符。它用于从给定的文件指针获取字符。
要检查的文件用作fgetc()函数的参数,它从文件中返回一个包含单个字符的字符串作为参数。
用法:
fgetc($file)
参数:PHP中的fgetc()函数仅接受一个参数$file。它指定需要从中提取字符的文件。
返回值:它从文件中返回一个包含单个字符的字符串作为参数。
错误与异常:
该功能未针对大型文件进行优化,因为它一次只能读取一个字符,并且完全读取一个长文件可能要花费很多时间。
如果多次使用fgetc()函数,则必须清除缓冲区。
fgetc()函数返回布尔值False,但是很多时候它返回一个非布尔值,该值的值为False。
以下示例程序旨在说明fgetc()函数。
程序1::在下面的程序中,名为gfg.txt的文件包含以下文本。
This is the first line.
This is the second line.
This is the third line.
// file is opened using fopen() function
$my_file = fopen("gfg.txt", "rw");
// Prints a single character from the
// opened file pointer
echo fgetc($my_file);
// file is closed using fclose() function
fclose($my_file);
?>
输出:
T
程序2::在下面的程序中,名为gfg.txt的文件包含以下文本。
This is the first line.
This is the second line.
This is the third line.
// file is opened using fopen() function
$my_file = fopen("gfg.txt", "rw");
// prints single character at a time
// until end of file is reached
while (! feof ($my_file))
{
echo fgetc($my_file);
}
// file is closed using fclose() function
fclose($my_file);
?>
输出:
This is the first line.
This is the second line.
This is the third line.