Is it allowed to write to a stream when it is not open in C ++

I have a code like this:

# in class definition
std::ofstream m_myFile;

## some where in code
m_myFile.open(filename);

and then in several places, I write to the file as follows:

m_myFile << "some data to file"<<std::endl;

This works well, now I need to add a flag to the system, which, if it is not installed, this file should not be created and written. I checked and I can run the application if I do this:

if(createFile)
{
      m_myFile.open(filename);
}

and leave the file entry as it is, and I don't get any runtime error in the windows. My question is that if I do not open the file and write to its stream, what is the standard behavior?

Should I get a runtime error or should the thread just forget about the data and not trigger a temporary error?

I am using Visual Studio 2013.

+1
source share
2

, . std::ofstream::badbit, .

, m_myFile.exceptions(std::ofstream::badbit), .

( std:: cout) , "dev null" streambuf, ( .rdbuf)

+3

, , .

0

All Articles