我试图使用由我拥有的缓冲区支持的std :: ostream。
当使用setp将std :: basic_streambuf添加到我的缓冲区时,一切正常,期望在编写为缓冲区分配的更多字节时,我希望设置eof。正在设置失败和坏位,而eof位不是。
这是预期的行为吗?
如果没有,我怎样才能设置eof位?
#include <iostream>
#include <streambuf>
template <typename char_type>
struct OStreamBufWrapper : public std::basic_streambuf<char_type, std::char_traits<char_type> >
{
OStreamBufWrapper(char_type* buffer, std::streamsize bufferLength)
{
// set the "put" pointer the start of the buffer and record its length.
this->setp(buffer, buffer + bufferLength);
}
};
int main(int argc, const char * argv[])
{
int bufsize = 10;
char *buf = new char[bufsize];
OStreamBufWrapper<char> ost(buf, bufsize);
std::ostream sb(&ost);
char i = 0;
while ( 1 )
{
sb.write(&i, 1);
if (!sb.good())
{
if (sb.fail())
std::cout << "fail\n";
if (sb.bad())
std::cout << "bad\n";
if (sb.eof())
std::cout << "eof\n";
break;
}
i++;
}
delete [] buf;
return 0;
}