search
Homephp教程php手册效率较高的php下读取文本文件的代码 原创

效率较高的php下读取文本文件的代码 原创

Jun 13, 2016 pm 12:27 PM
freadphpDowncodebyBitOriginalandbyteefficiencydocumenttextofcalculatereadlengthHigh

 fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止. 
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用.
读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中,
函数调用的形式为:
fgets(字符数组名,n,文件指针);
其中的n是一个正整数。
表示从文件中读出的字符串不超过 n-1个字符。
在读入的最后一个字符后加上串结束标志'\0'。
例如:fgets(str,n,fp);的意义是从fp所指的文件中读出n-1个字符送入字符数组str中。
按行读取)fgets()每次读入一行就会指向下一行.
所以把前面10行读进去了就清除. 
然后下一次fgets()就是第十一行了. 

fgets()怎么每次都是读一行?你怎么设定?
fread是用于整块数据的读写函数,可用来读写一组数据,如一个数组元素,一个结构变量的值等。
读数据块函数调用的一般形式为: 
fread(buffer,size,count,fp),其中buffer是一个指针,
在fread函数中,它表示存放输入数据的首地址。
size 表示数据块的字节数。
count 表示要读写的数据块块数。
fp 表示文件指针。 
例如: fread(fa,4,5,fp); 其意义是从fp所指的文件中,每次读4

fread -- 读取文件(可安全用于二进制文件)
在区分二进制文件和文本文件的系统上(如 Windows)打开文件时,
fopen() 函数的 mode 参数要加上 'b'。
$handle = fopen($filename, "rb"); 
本人使用的方法,如果你有好的方法,大家分享下啊

复制代码 代码如下:


$handle = @fopen($path, "r"); 
    if ($handle) {     
$buffer = fread($handle, 400); 
echo $buffer; 
fclose($handle); 

?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment