c++filefstreamfile-handling

What is the difference between ios::app and ios::noreplace modes in file handling?


I am studying File Handling in C++, but I am unable to see the difference between ios::app and ios::noreplace modes. Kindly help.

Note: ios::noreplace is a non-standard mode in some older version yet I am curious to know about it!


Solution

  • IIRC ios::noreplace was a nonstandard part of some old version of the MS run-time library. It was added to C++23, so there's one difference to begin with.

    The other difference is that ios::noreplace does not append at all: It simply fails if the file is present (thus the name no replace) and opens it only if it did not exist in the first place. ios::app opens the file if present or creates a new one and seeks to the end before each write.