High efficiency PHP Read text file code [original]_php Tips

Source: Internet
Author: User
Tags fread
Fread: Computes the length in byte bits, reads the data at the specified length and number, stops after it encounters the end or completes the specified length read.
Fgets: Full row reads, encounters carriage return or end stop. Used in text mode.
The function of the Fgets function is to read a string into a character array from the specified file.
The form of a function call is:
Fgets (character array name, n, file pointer);
where n is a positive integer.
Indicates that a string read from a file does not exceed n-1 characters.
After the last character read, add a string end sign '.
For example: Fgets (STR,N,FP) is meant to read n-1 characters into the character array str from the file referred to in FP.
Read by line Fgets () points to the next line each time a row is read.
So read the front 10 lines and clear them.
Then the next fgets () is the 11th line.

Fgets () How to read a line every time? How do you set it?
Fread is a read-write function for the entire block of data that can be used to read and write a set of data, such as an array element, the value of a struct variable, and so on.
The general form of a read block function call is:
Fread (BUFFER,SIZE,COUNT,FP), where buffer is a pointer,
In the Fread function, it represents the first address that holds the input data.
The size represents the number of bytes in the data block.
Count represents the number of blocks of data blocks to read and write.
FP represents a file pointer.
For example: Fread (FA,4,5,FP); The meaning of this is that from the file in the FP reference, read 4 each time

Fread-Read files (safe for binary files)
When you open a file on a system that distinguishes binaries and text files, such as Windows,
The mode parameter of the fopen () function is added ' B '.
$handle = fopen ($filename, "RB");
I use the method, if you have a good method, everybody share under AH
Copy Code code as follows:

<?php 
$handle  =  @fopen ($path,  "R"); 
    if  ($handle)  {     
$buffer  =  Fread ($handle,  400); 
echo  $buffer; 
fclose ($handle); 

?>

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: info-contact@alibabacloud.com 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.