Home  >  Q&A  >  body text

echo fseek($fp,10);Why does the output become 0?

echo fseek($fp,10);

益伦益伦2494 days ago1624

reply all(5)I'll reply

  • 卿立平

    卿立平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

    .

    reply
    2
  • 卿立平

    卿立平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.

    reply
    0
  • 小崔

    小崔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

    reply
    0
  • 小崔

    小崔2017-12-03 16:44:22

    The same question

    reply
    0
  • myfey

    myfey2017-11-27 13:59:22

    The 10th file position should be 0

    reply
    0
  • Cancelreply