Code for reading text files in php with high efficiency [original]

Source: Internet
Author: User
We mainly use the following two methods to differentiate fread from fgets. Note that fread: calculates the length in bytes and reads data according to the specified length and number of times, stops reading at the end or the specified length.
Fgets: Read the entire line. if a carriage return returns a line break or the end is stopped, use it in text mode.
The fgets function reads a string from a specified file to a character array,
Function calls are in the following format:
Fgets (character array name, n, file pointer );
N is a positive integer.
It indicates that the number of characters read from a file cannot exceed n-1.
Add the end string sign '\ 0' after the last character '.
For example, fgets (str, n, fp); reads n-1 characters from the file indicated by fp and sends them to the str array.
Read by row) fgets () each time a row is read, it will point to the next row.
So the first 10 rows are read and cleared.
Then, the next fgets () is 11th rows.

How does fgets () read a row every time? How do you set it?
Fread is a read/write function used to read and write a group of data, such as an array element and a value of a structure variable.
The call method for reading data block functions is as follows:
Fread (buffer, size, count, fp), where buffer is a pointer,
In the fread function, it indicates the first address for storing input data.
Size indicates the number of bytes of the data block.
Count indicates the number of data blocks to read and write.
Fp indicates the file pointer.
For example: fread (fa, fp); the meaning is from the file indicated by fp, each read 4

Fread -- read files (which can be safely used for binary files)
When a file is opened on a system that distinguishes binary files from text files (such as Windows,
'B' must be added to the mode parameter of the fopen () function '.
$ Handle = fopen ($ filename, "rb ");
If you have a good method, let's share it with you.
The code is as follows:
$ Handle = @ fopen ($ path, "r ");
If ($ handle ){
$ Buffer = fread ($ handle, 400 );
Echo $ buffer;
Fclose ($ handle );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.