PDA

View Full Version : seek function


cgibie
11-12-2005, 08:01 PM
I have a wording question, I don't understand what does this sentence tends to mean.

Since flock may force your CGI program to wait for another to finish writing to a file, you should also reset the file pointer, using the seek function.

I understand the flock function, and know what seek function is but I don't understand why need seek function for? I guess I don't know mind to know what seek function is again.

FishMonger
11-12-2005, 09:06 PM
I'm not sure how much detail you need, but while you're waiting for flock to place a lock on the file, another process could be writing to the file. That other process altered the location of the EOF marker from where it was when you opened it with open or sysopen. By using seek after you've obtained the lock, you can be relatively sure that you're at the end of the file.

cgibie
11-15-2005, 02:26 AM
Why sometimes it's SEEK_SET and sometime SEEK_END? I'm so confused about that beginning of file and EOF? Could you or anyone out there clarify me little bit.

Thank

FishMonger
11-15-2005, 02:56 AM
SEEK_SET => start of the file
SEEK_CUR => current position
SEEK_END => end of the file

http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlfunc.pod#seek

cgibie
11-15-2005, 05:38 PM
I know the meaning of those seek function, but I don't understand why sometime we put the file pointer to 1st, other time 2nd, and so on? Are there any rules associated with that?

Thanks

FishMonger
11-15-2005, 08:26 PM
If you know the meaning of the options to the seek function, but still don't know which to use and why, you probably won't be able to understand my explaination, beacuse I don't think I can explain it any better than the documentation that I pointed you to.

When you lock the file after openning it, you should set the pointer to where you want to start your reading or writting. Use 0 or SEEK_SET to position it at the beginning of the file. Use 2 or SEEK_END to position it at the end of the file. Use 1 or SEEK_CUR to set it at the current position which depending on whether you opened the file in read or write or append mode and whether another process (program) edited the file inbetween the time from your open call to obtaining the lock, it could be anywhere in the file.