What could cause a stream to enter the “bad” state?

前端 未结 2 483
有刺的猬
有刺的猬 2021-01-11 15:59

In C++, each stream has a bad bit:

This flag is set by operations performed on the stream when an error occurs while read or writing data

2条回答
  •  无人及你
    2021-01-11 16:13

    Take a look at the Apache C++ Standard Library User's Guide. Two potential causes for a badbit are listed there. I quote:

    Memory shortage: There is no memory available to create the buffer, or the buffer has size 0 for other reasons (such as being provided from outside the stream), or the stream cannot allocate memory for its own internal data.

    The underlying stream buffer throws an exception: The stream buffer might lose its integrity, as in memory shortage, or code conversion failure, or an unrecoverable read error from the external device. The stream buffer can indicate this loss of integrity by throwing an exception, which is caught by the stream and results in setting the badbit in the stream's state.

提交回复
热议问题