Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overwriting record in text file

Status
Not open for further replies.

mturnbo

Programmer
Apr 24, 2001
9
US
I am building a script that takes form input and writes it to a file in a specified packed format. Input from a second form need to update the record created from the first form. I am having trouble replacing the record with the new values.

When the second form is submitted I search for the email address in the record but unpacking each line into an array. If the email address matches then I replace elements of the array with the new values then re-pack the array. When I try to use syswrite to write to a specific location in the file, it seems to be overwriting the entire file with nothing.
 
The only real way that I have found to do it is

Open the file
Open a temp file
Read everything into an array
Compare the contents of the array to the form input
If no changes were made to that field, write the field to the temp file as is
If they changed, write the new values to the temp file
Close both files
Overwrite the original file with the temp file

That should take care of your problem and should run relatively quickly depending on the size of the file, etc.

Let me know how it goes,
Jim
 
Are you opening the file for input AND output (i.e. "+>$filename")? You can also use the tell command to find out where you are in the file, and the seek command to reposition to the beginning of the record before you write it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks.

I was opening the file for input and output. But it always got stuck at the point where I was using syswrite. I was able to get it working using the temp file technique suggested by JimJx. I originally planned to do it this way, but my concern was the effects of multiple people filling out the form simultaneously.
 
You can use the flock command to lock the file so that it cannot be simultaneously updated. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top