首先,我知道 ostrstream 已被贬低,但在这种情况下重构为 ostringstream 并不是一个真正的选择。
目前,我只是检查流在<<
操作后是否设置了错误位:
示例(是的,非常简化):
char* buf = new char[10];
std::ostrstream tempOut(buf, 10);
tempOut << "To many charactors to fit in the buffer";
if (tempOut.bad()) {
// Do what needs to be done to allocate a bigger buf
}
有没有更好的方法来检查并确保错误状态是由于溢出而不是其他问题?