No such thing as a small change | |
PerlMonks |
Re^4: Write in filesby Random_Walk (Prior) |
on Nov 18, 2013 at 12:53 UTC ( [id://1063078]=note: print w/replies, xml ) | Need Help?? |
It would be ugly. You would have to copy the file from the end of your current line, update the current line, then put the copied data back, after your new file position. I actually started to write some example code, but it is such a terrible idea I quickly gave up. You would probably load the entire file into an array, line per record, then go through the array, keeping a note of how many bytes into the file you are. When you update a record, seek to the end of the old record, before its \n, write the additional info plus new \n, then overwrite the rest of the file with the rest of the records in your array. If you really want to keep the same file, it would be better to build the entire new record set in memory, then blat it all out in one go, overwriting the old file. Power loss, or a crash, will probably cause data loss. If you really want to go mad, you could process one record at a time, then when you came to write out, read a few records from your file, write your data, then do a sort of read/write shuffle through the file to move everything up a few bytes. For this approach to work, you would have to be smoking something pretty special ;-) Cheers,
Pereant, qui ante nos nostra dixerunt!
In Section
Seekers of Perl Wisdom
|
|