in reply to Re: Re: Re: Re: Deleting a line out of text file
in thread Deleting a line out of text file
With a CGI script, you would have to manage the files yourself. And actually if this is going to be running on a server where you might ever have 2 people use this then you need to do some file management.
The basic procedure is to write the data into a new file then rename the file to the old name and use locking to prevent 2 people from trying to do that at the same time.
On Unixes you can use sysopen to pass O_CREAT|O_EXCL|O_WRONLY flags to the open which will fail if the temporary file exists already. You have to catch the failure and retry in a bit (like 1 second).
The other option is to use flock to do locking on the original file while you do the updating and unlock it when you are done. This has an option to wait but people don't use it because if the lock is left alive then all web accesses will wait while the user sits there looking at a blank web page. So this locking is usually checked non blocking which mirrors the sysopen behaviour. I think that flock is the best(only?) answer on windows and as such is more portable than sysopen which by definition is 'less portable'. Also the create exclusively is automic on Unix/Linux etc but I don't know on Windows, if its even available. Also, any one who has access to the file not via the CGI is free to ignore the locking completely via either method. Flocks go away if the process holding them dies whereas temp files do not so if you can arrange a exit/die on failire then the file gets unlocked.
In Summary, if you have more than 1 person ever do this, your file will turn up empty sooner or later.