Home Tutorial Php Examples fseek() example

 
 

fseek() example
Posted on: July 30, 2009 at 12:00 AM
Here you will learn about the function of fseek() example.

fseek ()

The fseek() function searches pointer in an open file and used to move from its current position to a new position, forward, backward as per specified by number of bytes. It returns 0 if succeed or -1 in the case of failure. 

If any file has been opened in the append mode, the data written on the file will also be appended. 

Syntax

fseek(file,offset,whence) 

Parameter

File - File is essential as it is the source. 

Offset - Offset is also essential that specifies the new position of the pointer

Whence - Whence is optional. It has three possible value.  

Possible values:

SEEK_SET - Set position equal to offset. It is a default value and when whence is not specified, it automatically as SEEK_SET
SEEK_CUR - Set position to current location plus offset
SEEK_END - Set position to EOF plus offset (to move to a position before EOF, the offset must be a negative value)

Example:

<?php
$filename = "New Text Document .txt";
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");

$fsize = filesize($filename);
$midfway = (int)( $fsize / 2 );

print "Middle point: $midfway <BR>\n";
fseek( $fp, $midfway );

$chunk = fread( $fp, ($fsize - $midfway) );
print $chunk;
?>

Related Tags for fseek() example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.