To replace the contents of a file, rather than expand it, you need to open it in
std::ios_base::out | std::ios_base::trunc
For output file streams, open mode is equivalent to out|trunc , that is, the trunc flag can be omitted.
However, for bidirectional file streams, trunc must be explicitly specified.
To expand the output file, use the flag std::ios_base::ate | std::ios_base::app std::ios_base::ate | std::ios_base::app .
Here the contents of the file are saved, since the trunc flag trunc not set, and the initial position of the file is at the end of the file.
However, in addition, the trunc flag can be set, and the contents of the file will be discarded, and the output will be executed at the end of the empty file.
source share