C++ 添加boost::动态过滤\u流过滤器
我正在使用过滤流来实现我的自定义归档格式。此格式应支持各种压缩算法,以便使用不同的方法压缩每个文件 为此,我保留了一个用于读取(或写入)的全局流,并根据需要添加或删除过滤器。然而,目前尚不清楚是否可以动态添加和删除过滤器 本质上,我是在尝试这样做:C++ 添加boost::动态过滤\u流过滤器,c++,boost,iostream,C++,Boost,Iostream,我正在使用过滤流来实现我的自定义归档格式。此格式应支持各种压缩算法,以便使用不同的方法压缩每个文件 为此,我保留了一个用于读取(或写入)的全局流,并根据需要添加或删除过滤器。然而,目前尚不清楚是否可以动态添加和删除过滤器 本质上,我是在尝试这样做: void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filters ) { // create the filtered stream b
void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filters )
{
// create the filtered stream
boost::iostreams::filtering_istream in;
if ( filters & FILTERS_BZIP )
{
in.push( boost::iostreams::bzip2_decompressor() );
}
// add the source stream
in.push( inputStream );
// read file content
in.read( &mFileContent[0], mFileSize );
}
我使用相同的inputStream为每个文件调用readFromStream。然而,即使不使用任何过滤器,我也会一直这样胡言乱语。当我直接使用inputStream时,文件的读取状态为ok
我做错了什么?我知道已经有一段时间了,但我只是偶然发现了这个问题。因此,答案是将整个代码部分放在一个新的范围内:
void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filter
{
// new scope
{
// create the filtered stream
boost::iostreams::filtering_istream in;
if ( filters & FILTERS_BZIP )
{
in.push( boost::iostreams::bzip2_decompressor() );
}
// add the source stream
in.push( inputStream );
// read file content
in.read( &mFileContent[0], mFileSize );
}
}
这可能是因为我当时使用的编译器(MS Visual Studio的某个版本)中存在一些错误而必需的。我知道已经有一段时间了,但我只是偶然发现了这个问题。因此,答案是将整个代码部分放在一个新的范围内:
void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filter
{
// new scope
{
// create the filtered stream
boost::iostreams::filtering_istream in;
if ( filters & FILTERS_BZIP )
{
in.push( boost::iostreams::bzip2_decompressor() );
}
// add the source stream
in.push( inputStream );
// read file content
in.read( &mFileContent[0], mFileSize );
}
}
这可能是由于我当时使用的编译器(MSVisualStudio的某个版本)中存在一些错误而必需的