note 17897 added to function.fseek
| From: | dan at daniellampert dot com | Date: | Tue, 01 Jan 2002 19:54:11 +0000 |
| Subject: | note 17897 added to function.fseek | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24034@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.fseek.php
http://master.php.net/manage/user-notes.php?action=edit+17897
http://master.php.net/manage/user-notes.php?action=delete+17897
http://master.php.net/manage/user-notes.php?action=reject+17897