How to overwrite a text file?
Flag "w"
should overwrite the existing file.
See this documentation for details, specifically:
The mode argument points to a string. If the string is one of the following, the file shall be opened in the indicated mode. Otherwise, the behavior is undefined.
- r or rb: Open file for reading.
- w or wb: Truncate to zero length or create file for writing.
- a or ab: Append; open or create file for writing at end-of-file.
- r+ or rb+ or r+b: Open file for update (reading and writing).
- w+ or wb+ or w+b: Truncate to zero length or create file for update.
- a+ or ab+ or a+b: Append; open or create file for update, writing at end-of-file.