Read files similar to C language for parsing

Source: Internet
Author: User
Tags fread

$log _file_name = ' d:/static/develop/kuai_zhi/acagrid.com/public/logs/'. Date (' Ym '). ' /'. Date (' d '). ' _error.log ';

$log _file_name = ' d:/static/develop/kuai_zhi/acagrid.com/public/logs/201701/19_error.log ';


if (!file_exists ($log _file_name)) return;


$handle = fopen ($log _file_name, ' RB ');
if (FALSE = = = $handle) {
Exit ("Failed to open stream to URL");
}

$stream = Fread ($handle, $length);//read n bytes in length from the current pointer position of the file

Resets the position of the file pointer. Specifies the position of the pointer after the pointer position has been modified. Read the file, followed by the read from this location
Fseek ($handle, 105);

Fgets indicates that one line of the file is read each time


$error _log_array = [];
while (($line = fgets ($handle))!==false) {
Read one line at a time

The match appears in the [1],TP log with this type of fatal error
if (Preg_match ("/\[1\]/", $line)) {
$error _log_array[] = $line;
}

}

Fclose ($handle);


A few points to note:

1. If you are using fwrite, be careful to avoid emptying the contents of the original file. The key is the way fopen is opened. R or W.

If the Append method is used, the a tag.

2, fopen, pay attention to determine whether to open the file successfully. Avoid using feof to enter the dead loop. because of this function, when passed in is not a pointer, this function will always return false

Feof's intention is to determine whether the end of the file. Returns true if it is the end. Not end returns false. If an illegal pointer is passed in, it is never the end of the file and always returns

False

The feof () function, when passed in is not a pointer type, use the following to determine a dead loop

while (!feof ($fp)) {

}


3, Fread and fgets. Reads the file one line at a line, then uses Fgets. is not read by line, it is read with Fread ().

Note This detail: if there is no more content, it returns false, that is, two cases, if the contents are empty. Also returns false. When reading to the end of the file, these two functions also return False (no wonder we use feof () so that we do not find this detail, because this function has helped us to determine the end of the file)

4, use the Append method (that is, a mark) to open the file, to note that in this way, can not read the contents of the file, only to write to the file. So Fread () for this handle will get false.

The summary is, if just read the contents of the file, only read the way open, if it is to write new content in, then use a to open the way

Now understand why you have to differentiate between multiple patterns. I used to think it was useless. Now it seems that the way you open the file determines what you can do with the file (add new content or read the content). )

Read files similar to C language for parsing

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.