卿立平2018-01-28 21:30:33
You can check the php7 manual. The fseek function is a function that moves the pointer backward by a specified byte. However, there are some requirements for this pointer movement, and there are three results: 1. Adding the specified bytes backwards at the set position (beginning of the file); 2. Adding the specified bytes to the file at the set position (beginning of the file). Add the specified byte to the end. 3. The set position (the predetermined position of the file pointer) is equal to the number of bytes to be added.
The return value of this function is: 0 if the increase is successful; otherwise -1 is returned.
At this time we use fseek($fp,10). The understanding at this time should be that the pointer returns to the beginning of the file and then moves backward 10 bytes.
Let’s test it. I have a file called “mylove.txt” with the following content: “We must keep in mind that the people’s yearning for a better life is our goal, adhere to the people-centered development idea, and strive to do a good job We must ensure and improve people’s livelihood, continuously enhance the people’s sense of gain, happiness, and security, and continuously promote the common prosperity of all people.” On October 25, 2017, General Secretary Xi Jinping, a member of the Standing Committee of the Political Bureau of the 19th CPC Central Committee, met with Chinese and foreign officials. The speeches made by reporters when they met were very impactful. ”
Execution program:
<?php
$fp = fopen('D:\phpStudy\PHPTutorial\WWW\demo\mylove.txt' , 'r+ ');
//Read 10 characters
echo fread($fp, 10); //When reading a file through fread, when the file pointer has read the specified length string , the pointer will stay at the specified length
//The pointer is set back to the beginning
rewind($fp);
echo '<br/> ';
echo fread($fp,10); // After reading, the pointer stops at the 10th byte. The output content is "We must remember"
echo. '<br/>';
echo fseek($fp,20); //The file pointer moves backward 20 characters from the file header. This indicates that the operation is successful, so the return value is 0. But the pointer position at this time should be at the 20th character position
echo '<br/>';
echo fread($fp,10);// This. At this time, the length of 10 bytes should be read backwards from the 20th byte character, so the output should be "Longing for Life"
echo '<br/>';
?>
The output result of the program is:
We must keep in mind
We must keep in mind
0
The yearning for life
Based on this case, you should be able to better understand the fseek () function. It is best to check the manual to understand
卿立平2018-01-28 21:18:53
You can check the php7 manual. The fseek function is a function that moves the pointer backward by a specified byte. However, this pointer movement has some requirements, and there are three results: 1. Adding the specified byte to the end of the file at the set position; 2. Adding the specified byte to the end of the file at the set position, 3. The set position is equal to the number of bytes to be added.
The return value of this function is: 0 if the increase is successful; otherwise -1 is returned.
小崔2017-12-05 16:32:47
The fseek() function returns 0 if the read is successful, otherwise it returns -1. In the case, fseek() reads the data