|
|
 |
(PHP 3, PHP 4 ) fseek -- Modifie le pointeur de fichier. Descriptionint fseek ( int fp, int offset)
fseek() modifie le curseur de position
dans le fichier fp. La nouvelle position
mesurée en octets à partir du début du fichier,
est obtenue en additionnant la distance offset
à la position whence. Ce paramètre
peut prendre les valeurs suivantes :
|
SEEK_SET - La position finale vaut offset octets.
| |
SEEK_CUR - La position finale vaut la position courante
ajoutée à offset octets.
| |
SEEK_END - La position finale vaut la position courante par rapport à
la fin du fichier, ajoutée de offset.
|
Si whence n'est pas spécifiée, il
vaut par défaut SEEK_SET.
fseek() retourne TRUE en cas de
succès, et sinon -1. Notez que positionner le pointeur au
delà de la fin du fichier n'est pas une erreur.
fseek() ne peut pas être utilisé sur les pointeurs
retournés par fopen() s'ils sont au format HTTP ou FTP.
Voir aussi
ftell() et
rewind().
User Contributed Notes fseek |
 |
dan at daniellampert dot com
01-Jan-2002 09:54 |
|
For all first-time users of the fseek() function, remember these three
things:
1. to use a programming expression, fseek() is "base
0", so to prepare the file for writing at character 1, you'd say
fseek($fp,0); and to prepare the file for writing at character $num, you'd
say fseek($fp,($num-1));
2. here's the formula for accessing
fixed-length records in a file (you need to seek the position of the end of
the previous record):
/* assumes the desired record number is in
$rec_num */
/* assumes the record length is in $rec_len */
$pos
= ( ($rec_num-1) * $rec_len );
fseek($fp,$pos);
3. if you're
using fseek() to write data to a file, remember to open the file in
"r+" mode, example:
$fp=fopen($filename,"r+");
Don't open the file in mode
"a" (for append), because it puts the file pointer at the end of
the file and doesn't let you fseek earlier positions in the file (it didn't
for me!). Also, don't open the file in mode "w" -- although this
puts you at the beginning of the file -- because it wipes out all data in
the file.
Hope this helps.
|
|
|
25-Aug-2002 12:12 |
|
The following call moves to the end of file (i.e. just after the last byte
of the file):
fseek($fp, 0, SEEK_END);
It can be used to
tell the size of an opened file when the file name is unknown and can't be
used with the filesize() function:
fseek($fp, 0,
SEEK_END); $filesize = ftell($fp);
The following call moves
to the begining of file:
fseek($fp, 0, SEEK_SET);
It is
equivalent to:
rewind($fp);
|
|
|
16-Sep-2002 08:25 |
|
Don't use filesize() on files that may be accessed and updated by parallel
processes or threads (as the filesize() return value is maintained in a
cache). Instead lock the opened file and use fseek($fp,0,SEEK_END) and
ftell($fp) to get the actual filesize if you need to perform a fread() call
to read the whole file...
|
|
 |
| |