in reply to Re: read file line by line then do something and write to same file
in thread read file line by line then do something and write to same file
I wouldn't call that the general solution, as one of its most likely failure modes would result in data loss (imagine loss of power before the new file is written, or an inability to write the new file).
A more common pattern is the one that Perl's -i switch accomplishes for one-liners: Rename the input file, open the output file in the name of the original input file, open the renamed input file, iterate over the input, write to the output, close the output, and as a final step, unlink the renamed input.
With that pattern rename is atomic, and unlink happens only after the new output file is successfully closed.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: read file line by line then do something and write to same file
by wjw (Priest) on Jun 15, 2014 at 23:59 UTC | |
|
Re^3: read file line by line then do something and write to same file
by oiskuu (Hermit) on Jun 16, 2014 at 19:36 UTC | |
by davido (Cardinal) on Jun 16, 2014 at 19:44 UTC |