Filestream open mode

If you intend to open an fstream for output in ios::app mode, do you really need to also specify ios::out as well? For my specific compiler, using just ios::app and not ios::out | ios::app allowed me to output to the file. Doesn't ios::app already assume that you are opening the file for output?
no, that’s only for ios::trunc.
Edit: I mean yes
Last edited on
Hello TheToaster,

It would depend on if you defined the file stream as a "fstream" or an "ofstream".

For "fstream" you need to tell it if it is for input or output. At least that has been the way I have understood it. I will also say that I have never tried to use just "std::ios::app" when opening an output file stream. I will have to give that a try.

It would help if you would mention your IDE and compiler as there are differences.

Hope that helps,

I am referring to an fstream. I am using TDM-gcc MinGW with code::blocks, but I also tested this with regular g++ and it worked.
If you say std::fstream out(std::ios::app); it does not require you to include std::ios::out, it does require it for std::ios::trunc.
Hello TheToaster,

Sorry for the delay. I have not been feeling well.

I tried this with VS2017 compiled to the 14 standards and it worked for me as well.

If you read , at least as a start you will notice that nothing is said about a "fstream" having a default based on other mode flags.

It has been my experience that when using a "fstream" it is best to start with telling the stream if it is for "in", "out or both before you add any other mode flags.

This is one of times when something is found by accident or testing, but do not count on it working all the time. As soon as you do it will fail.

Hope that helps,

Registered users can post here. Sign in or register to post.