C++ 如何使用boost::iostreams::file\u descriptor\u source读取文件?
我需要在我的应用程序中使用boost::iostreams::file\u descriptor::handle\u type。 我尝试使用下面的代码读取一个文件,但在这段时间内它一直在循环 循环(in.readsome()返回0)C++ 如何使用boost::iostreams::file\u descriptor\u source读取文件?,c++,boost,boost-iostreams,C++,Boost,Boost Iostreams,我需要在我的应用程序中使用boost::iostreams::file\u descriptor::handle\u type。 我尝试使用下面的代码读取一个文件,但在这段时间内它一直在循环 循环(in.readsome()返回0) 使用名称空间boost::iostreams file_descriptor_source source( "data.bin", never_close_handle); stream_buffer<file_descriptor_source> s
使用名称空间boost::iostreams
file_descriptor_source source( "data.bin", never_close_handle);
stream_buffer<file_descriptor_source> stream(source);
std::istream in(&stream);
char buffer[1025];
memset(buffer, 0, sizeof(buffer));
while ( !in.eof() )
{
streamsize read = in.readsome(&buffer[0], sizeof(buffer) - 1);
}
文件描述符源代码(“data.bin”,永不关闭句柄);
流\缓冲流(源);
std::istream in(&stream);
字符缓冲区[1025];
memset(buffer,0,sizeof(buffer));
而(!in.eof())
{
streamsize read=in.readsome(&buffer[0],sizeof(buffer)-1);
}
到达文件末尾不是您应该检查的唯一条件,因为可能还有其他情况您必须处理。例如,您可以:
- 检查
是否发生了不可恢复的错误bad()
- 检查与
关联的流上是否发生错误fail()
while (in) {
streamsize read = in.readsome(buffer, sizeof(buffer) - 1);
if (read > 0) {
...
}
}
退出循环后,可以使用
eof()
,fail()
等检查退出循环的原因。以下语句不会从流中读取任何内容
streamsize read=in.readsome(&buffer[0],sizeof(buffer)-1)代码>
并且总是返回0,这就是为什么它永远循环
初始化“in”对象时出错。
我不知道怎么了