PHP | fgetc()函数

PHP中的fgetc()函数是一个内置函数,用于从打开的文件中返回单个字符。它用于从给定的文件指针中获取字符。

null

要检查的文件用作fgetc()函数的参数,它返回一个字符串,其中包含用作参数的文件中的单个字符。

语法:

fgetc($file)

参数: PHP中的fgetc()函数只接受一个参数 $file 。它指定需要从中提取字符的文件。

返回值: 它从用作参数的文件中返回一个包含单个字符的字符串。

错误和例外 :

  1. 由于该功能一次只能读取一个字符,并且可能需要很多时间才能完全读取一个长文件,因此它没有针对大文件进行优化。
  2. 如果多次使用fgetc()函数,则必须清除缓冲区。
  3. 函数的作用是:返回布尔值False,但很多时候,它会返回一个计算结果为False的非布尔值。

下面的程序演示了fgetc()函数。

方案1 :在下面的程序中,名为 gfg。txt 包含以下文本。

这是第一行。 这是第二行。 这是第三行。

<?php
// 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 包含以下文本。

这是第一行。 这是第二行。 这是第三行。

<?php
// 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.

参考: http://php.net/manual/en/function.fgetc.php

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享