PHP Move file pointer ftell (), fseek (), rewind () function Summary _php tips

Source: Internet
Author: User
Tags fread rewind

In the process of reading and writing files, it is sometimes necessary to jump in a file, read in different locations, and write data to a different location. For example, you would need to move a file pointer to save data using a file emulation database. The position of the pointer is measured by the number of bytes starting at the head of the file. When you open a file by default in a different mode, the file pointer is usually at the beginning or end of the file, and the file pointer can be manipulated by the Ftell (), fseek (), and Rewind () three functions, and their prototypes are as follows:

Copy Code code as follows:

int Ftell (resource handle)//returns the current position of the file pointer
int fseek (Resource hanlde,int offset[,int whence])//Move file pointer to specified location
BOOL Rewind (resource handle)//move file pointer to beginning of file

When using these functions, you must provide a legitimate file pointer that is opened with the fopen () function. function Ftell () Gets the offset from the current position of the file pointer in the specified resource; the function rewind () Moves the file pointer back to the beginning of the specified resource, and the function fseek () function moves the pointer to the position specified by the second argument, If the third optional parameter whence is not provided, the position is set to offset byte at the beginning of the file. Otherwise, the third argument whence can be set to three possible values, which affects the position of the pointer.

★seek_cur: Sets the offset byte provided by the position of the pointer to the current position plus the second parameter.
★seek_end: Sets the pointer position to EOF plus offset bytes. In this case, offset must be set to a negative value.
★seek_set: Sets the position of the pointer at the offset byte. This is the same effect as ignoring the third parameter whence.

If the fseek () function succeeds, it returns 0, and the failure returns-1. If you open the file in Append mode "a" or "A +", any data that is written to the file is appended to the back and does not position the file pointer. The code looks like this:

Copy Code code as follows:

<?php
$fp = fopen (' data.txt ', ' r ') or Die ("File open failed");

Echo Ftell ($fp). "         <br> "; Outputs the default position of the pointer to the file you just opened, and the pointer is 0 at the beginning of the file
Echo fread ($FP, 10). "        <br> "; Read the first 10 characters output in the file, the pointer position has changed
Echo Ftell ($fp). "           <br> "; After reading the first 10 characters of the file, the position of the pointer moves at the 10th byte

Fseek ($fp, 100,seek_cur); And move the pointer to the bottom 10 byte position.
Echo Ftell ($FP); The location of the file is at 110 bytes
Echo fread ($FP, 10). "     <br> "; Reads a string from 110 to 120 bytes, where the pointer is read at 120

Fseek ($fp, -10,seek_end); And move the pointer to the bottom 10 byte position.
Echo fread ($FP, 10). "        <br> "; Last 10 characters in output file

Rewind ($FP); Move the file pointer to the beginning of the file again
Echo Ftell ($FP); Pointer at the beginning of the file, output 0

Fclose ($FP);
?>

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.