[英]gzip compressed file streams in C++
我的装载机看起来像这样:
void LispAna::loadF() {
formatives.rehash(2991);
std::ifstream fin("data.txt");
formCondition st;
std::string key;
std::string tmp;
int itmp;
for (std::string row; getline(fin, row, '\n');) {
//"a\t0\txx\tA\n"
std::istringstream ss(row);
getline(ss, key, '\t');
//pos
getline(ss, tmp, '\t');
st.pos = *(tmp.c_str());
//types
getline(ss, tmp, '\t');
st.types = tmp;
//condition
getline(ss, tmp, '\t');
st.condition = tmp;
//if(ok)?
formatives.insert(std::pair<std::string, formCondition>(key, st));
}
}
我喜欢替换std::ifstream
巫婆自己创造的zifstream
。 这可以很容易地用于读取zlib
压缩文本文件。
zifstream fin("data.txt.gz");
//..
for (std::string row; getline(fin, row, '\n');) {
//..
}
我有tinfl.c
v1.11。 而且我没有一个线索女巫类来扩展以及实现它的实现功能。 我不需要寻求。 只需简单的线性读数和一些基本的错误检查。 甚至不需要完整的头支持,数据文件是自己做的。
首先,我认为Boost已经拥有了你想要的东西。 如果你可以使用它,那肯定比自己重写代码更简单。
否则:您需要定义一个进行压缩的过滤streambuf,并将压缩数据转发到另一个streambuf(几乎可以肯定是以二进制模式打开的filebuf
)。 只需从std::streambuf
派生,指向您班级中的最终目标streambuf
。 至少,你必须覆盖函数overflow
; 您可能还想覆盖xsputn
。
实际的zifstream
非常简单:类似于:
class zifstream : private std::filebuf, private zistreambuf, public std::istream
{
public:
zifstream( std::string const& filename )
: std::filebuf( filename.c_str(), std::ios_base::in | std::ios_base::binary )
, zistreambuf( static_cast<std::filebuf>( this ) )
, std::istream( static_cast<zistreambuf>( this ) )
{
}
};
可能就足够了。 (从std::istream
派生的标准类还提供了一个rdbuf
函数,该函数隐藏了基类中的一个,并返回派生类型。在一些特殊情况下,这更像是“很好”,而不是真的必要。)
请注意,如果您使用"C"
以外的区域设置进行阅读,则必须在过滤缓冲区中进行代码转换; 在filebuf中执行它将无法正常工作。 (再一次,Boost有必要的支持。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.