3 ms·
Here is a library suggestion: a "m" mode for fopen. "m" is the same as "w", but does not truncate the file. In POSIX terms, it doesn't add O_TRUNC to the flags
by kazinator 7y ago
Here is a library suggestion: a "m" mode for fopen.
"m" is the same as "w", but does not truncate the file. In POSIX terms, it doesn't add O_TRUNC to the flags.
There is "r+", of course; but "r+" requires that the file exists already. In POSIX terms, "r+" does not include the O_CREAT flag.
fopen("foo", "m") creates the file if it does not exist, and opens it for writing. The stream is positioned at the beginning of the file without truncating it.
We can sort of emulate it with fopen("foo", "a"), then fclose, then open with "r+".