Josh C
Josh C

Reputation: 1111

What happens to the data inserted into an unopen stream?

My intuitive feeling is that the data is thrown away entirely. I cannot seem to find a source with which to verify this suspicion.

What happens to data inserted into an unopen stream? (eg. std::ofstream)

Is the data discarded? Perhaps it is stored in a buffer until the stream is opened? Perhaps something else?

Upvotes: 1

Views: 42

Answers (2)

user2249683
user2249683

Reputation:

If the output stream is in a fail state (eg.: not open) nothing happens to the stream - the request to output/buffer data is ignored entirely.

Note: If the exception std::ios_base::badbit is enabled, it will be thrown.

Upvotes: 1

David G
David G

Reputation: 96835

In the standard "remarks" of all the file stream buffer methods that correspond to operations on the buffer it indicates that if is_open() == false, the function always fails. Failure is defined as returning traits_type::eof(). This special value is caught by higher-level IO functions which in turn set std::ios_base::badbit flag in the stream state.

Upvotes: 1

Related Questions