Is it allowed to write to a ofstream when it is not opened in c++

懵懂的女人 提交于 2019-12-01 09:29:09

The standard behavior is that the first write fails. This sets the std::ofstream::badbit and further writes are silently ignored.

This silent failure could be changed to an exception by setting m_myFile.exceptions(std::ofstream::badbit), but it's off by default.

You can make any stream (even std::cout) discard its output by creating a "dev null" streambuf and then switching your stream to that buffer (via .rdbuf)

If you don't open the file you have a stream with a default constructed file buffer that is being written to and will get discarded when the ofstream is discarded.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!