Jump to content

While reading text file, rewind back to beginning?


coltsith

Recommended Posts

Hi there,

 

I'm currently trying to read a list of values in a text file, and then at the end, go back to the very beginning and write a new list of values.

 

I've tried Rewind(file) but that only seems to rewind the reader back to the beginning of the current line. How do I make it rewind all the way back to the beginning of the entire file?

 

I need to do this because I need this operation synchronized and am using fLock(). And if I close the file and open it again to write, there could potentially be problems in between the closing of the first read, and the opening of the second:

 
flock($a)
fopen($a, read)
fclose($a)
                          // < ---- Not synchronized; Someone else calling this script could potentially open the file
                          // here.
flock($a)
fopen($a, write)
fclose($a)

 

What I'd like to be able to do is:

 

 
flock($a)
fopen($a, read+write)
// read down list
fRewindBackToBeginningOfFile($a)
// write new list
fclose($a)

 

Thanks for any help you can give!

Link to comment
Share on other sites

Flock is used on a file handle, not a filename. The proper way to use flock is with a seperate lock file...so something like:

 

 

<?php
  $file = 'myfile.txt';

  $FL = f_open("{$file}.lck","a+");  //Open Lock File
  f_lock($FL,LOCK_EX);  //Start lock

  $FH = f_open($file,'w');  //Open file
  // do operations here

  f_close($FH);  //Close File
  f_close($FL);  //Close Lock
?>

 

NOTE: To post the code, I had to add underscores in the function names. Make sure you remove those.

Link to comment
Share on other sites

yes...it's just an empty file

 

Since you can't lock a file until after you get a File Handle open, it needs to be a separate file that gets open and locked before the actual file is worked on. Check out the different kind of locks you can make. If one piece of code is only reading the file, do a shared lock (multiple instance can read at the same time. Then use the exclusive lock with the piece of code that writes, to make sure no one else is accessing the file while you are changing it.

Link to comment
Share on other sites

Okay, thanks! I've got the script written and basically working. One last question:

 

I'm using this script to write a table, and I want another script to read from it and put the values into an Html Table.

 

Do I have to use a lock for the reading script? Can I just read from the file with no locks? I'm not worried about the user viewing information that's out of date and needs to refresh, I'm only worried about it possibly screwing up the writing process if the write script is being called.

Link to comment
Share on other sites

Use a shared lock(LOCK_SH) on the read script and an exclusive lock(LOCK_EX) on the write script. That way scripts will be able to simultaneously read with no problem, until the file is opened for writing. Then the read scripts will wait while the write script is working.

Link to comment
Share on other sites

Yeah, there is a chance they will read the data mid write. The write should take a matter of milliseconds, so you shouldn't even notice it in the read script. And the change of something reading it while you are writing are very small. But if you are looking to be safe, the above way is the best approach (in my opinion).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.