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
In reply to Re^2: read file line by line then do something and write to same file
by davido
in thread read file line by line then do something and write to same file
by john.tm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |