php fgets() 和 fread() - 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2751632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:33:47  来源:igfitidea点击:

fgets() and fread() - What is the difference?

phpsmtpstreamfreadfgets

提问by Alix Axel

I understand the differences between fgets()and fgetss()but I don't get the difference between fgets()and fread(), can someone please clarify this subject? Which one is faster? Thanks!

我明白之间的差别fgets()fgetss(),但我不明白之间的差别fgets()fread(),有人可以澄清这个问题?哪个更快?谢谢!

回答by Pascal MARTIN

fgetsreads a line-- i.e. it will stop at a newline.

fgets读取一行——即它会停在换行符处。

freadreads raw data-- it will stop after a specified (or default)number of bytes, independently of any newline that might or might not be present.

fread读取原始数据——它将在指定(或默认)字节数后停止,独立于可能存在或不存在的任何换行符。


Speed is not a reason to use one over the other, as those two functions just don't do the same thing :


速度不是使用一个而不是另一个的理由,因为这两个功能只是不做同样的事情:

  • If you want to read a line, from a text file, then use fgets
  • If you want to read some data (not necessarily a line)from a file, then use fread.
  • 如果要从文本文件中读取一行,请使用 fgets
  • 如果你想从文件中读取一些数据(不一定是一行),那么使用fread.

回答by zloctb

fread() for binary data and fread has a limit on how many chars you can read

fread() 用于二进制数据,并且 fread 对您可以读取的字符数有限制

$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
    $buffer = fread($source_file, 5);  
   var_dump($buffer); //return string with length 5 chars!
}

Number 5 is length bytes have been read .

数字 5 是已读取的长度字节。

回答by Pritom Das

The function fgets reads a single line from a text file. It is reading so long until the end of the current line (or the end of the file) is reached. Therefore, if you would like to read one line from a text file, you should use fgets. The function fread not only reads until the end of the line but to the end of the file [e.g. fread($handle)] or as many bytes as specified as a parameter [e.g. fread($handle, 1024)]. So, if you would like to read a complete file, no matter whether it is a text file with all containing lines or arbitrary raw data from a file, you should use fread.

函数 fgets 从文本文件中读取一行。它正在读取很长时间,直到到达当前行的末尾(或文件的末尾)。因此,如果您想从文本文件中读取一行,您应该使用 fgets。函数 fread 不仅读取到行尾,而且读取到文件末尾 [例如 fread($handle)] 或作为参数指定的字节数 [例如 fread($handle, 1024)]。因此,如果您想读取一个完整的文件,无论是包含所有行的文本文件还是来自文件的任意原始数据,都应该使用 fread。

回答by Abhishek Pai

Both the functions are used to read data from files

这两个函数都用于从文件中读取数据

fgets($filename, $bytes) fgets usually reads $bytes-1 amount of data and stops at a newline or an EOF(end-of-file) whichever comes first. If the bytes are not specified, then the default value is 1024 bytes.

fgets($filename, $bytes) fgets 通常读取 $bytes-1 的数据量并在换行符或 EOF(文件结尾)处停止,以先到者为准。如果未指定字节,则默认值为 1024 字节。

fread($filename, $bytes) fread reads exactly $bytes amount of data and stops only at EOF.

fread($filename, $bytes) fread 准确读取 $bytes 的数据量并且仅在 EOF 处停止。