【发布时间】:2018-09-09 16:19:22
【问题描述】:
我不知道,是否可以将 std::basic_ifstream 和 std::basic_ofstream 与 std::basic_filebuf 的自定义实现一起使用?
按 64KB 大小的块读取文件并在内部检查块的某些哈希值的输入文件流的实现有多复杂?例如,如果哈希无效,则会抛出 corruption_exception。输出文件流在其后写入块和哈希值。
我找到了一些创建 std::ifstream 的示例,然后创建另一个从中读取并进行额外处理的流:
std::ifstream infile("test.img");
decompress_stream in(infile, 288);
char data[144 * 128];
in.read(data, 144 * 128);
infile.close();
但起初我希望它应该是这样的(没有额外的流):
std::ifstrem in;
in.setbuffer(new MyBuffer());
in.read();
MyBuffer::underflow()
{
//read from original buffer
if (hash != calculated_sash) throw curruption_exception();
//return the data with omitted hash.
}
这可能吗?
【问题讨论】:
-
不使用外部库的方法是从
std::basic_filebuf派生一个类,并使用该类型的对象作为basic_istream的缓冲区。
标签: c++