- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想不通,是否可以将 std::basic_ifstream 和 std::basic_ofstream 与 std::basic_filebuf 的自定义实现一起使用?
按 64KB 大小的 block 读取文件并在内部检查 block 的某些哈希值的输入文件流的实现有多复杂?例如,如果哈希无效,它会抛出 corruption_exception。输出文件流在其后写入 block 和哈希值。
我找到了一些创建 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
和 std::basic_[io]stream
的组合。流接口(interface)允许通过 rdbuf()
方法访问 std::basic_streambuf
。因此,您可以用另一个替换文件流流缓冲区。但是,它与原始文件缓冲区没有任何关系。
由于您拥有的流缓冲区是一个过滤流缓冲区,因此使用流构造它并让构造函数注入(inject)过滤器可能是合理的,即像这样的东西(我省略了模板,因为这些与此无关讨论,但可以很容易地添加):
class filterbuf
: public std::streambuf {
std::istream* istream = nullptr;
std::ostream* ostream = nullptr;
std::streambuf * sbuf;
// override virtual functions as needed
public:
explicit filterbuf(std::istream& in)
: istream(&in)
, sbuf(istream->rdbuf(this)) {
}
explict filterbuf(std::ostream& out)
: ostream(&out)
, sbuf(ostream->rdbuf(this)) {
}
explicit filebuf(std::iostream& inout)
: istream(&inout)
, sbuf(istream->rdbuf(this)) {
}
~filebuf() {
istream && istream->rdbuf(sbuf);
ostream && ostream->rdbuf(sbuf);
}
};
在析构函数中恢复流缓冲区的要点是 std::ostream
析构函数调用对象上的 flush()
并且自定义流缓冲区消失了那个时候。
过滤器将像这样使用:
std::istream fin(“whatever”);
filterbuf buf(fin);
if (fin >> whatever) {
...
}
关于c++ - 是否可以将 std::basic_ifstream 和 std::basic_ofstream 与自定义缓冲区一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246409/
这是我第一次使用QString,我不知道如何解决此错误: Personanetscape.cpp:16:错误:没有匹配的函数可以调用“std::basic_ifstream::basic_ifstre
我正在尝试编写一个程序来打开一个文件并计算该文件中以空格分隔的单词数。 #include #include #include using namespace std; int main() {
从这段代码构建的程序: #include using std::basic_ifstream; #include using std::streamsize; #include using Ze
我有一个二进制文件。上半部分存储了2288*2288个经度浮点值,同样数量的纬度浮点值占据了下半部分。我使用以下代码将它们加载到浮点 vector 中。它可以像魅力一样运行,但给了我不正确的结果。关于
我应该编写一个程序,合并两个文件中的数字并将所有数字写入第三个文件。该程序从两个不同的文件中获取输入并将其输出写入第三个文件。每个输入文件都包含一个按从小到大排序的 int 类型数字列表。程序运行后,
这可能吗?我一直在尝试读取一个包含俄语的简单文件,但显然无法正常工作。 我调用了 file.imbue(loc)(此时,loc 是正确的,Russian_Russia.1251)。而 buf 的类型是
我正在尝试使用以下代码从文件中读取数据。 (请注意,您需要在 GCC 上启用 C++11 功能才能进行此编译。) #include typedef unsigned char byte; int m
我想不通,是否可以将 std::basic_ifstream 和 std::basic_ofstream 与 std::basic_filebuf 的自定义实现一起使用? 按 64KB 大小的 blo
我在图像处理的上下文中学习 IO,这是我第一次不得不将流用于二进制文件而不是格式化/文本文件。我正在尝试使用带有模板编程的重载流插入运算符将位图的前 14 个字符读入结构。 #include #in
以下代码按预期工作。源代码,文件“file.txt”和“out.txt”都是用utf8编码的。但是当我改变时它不起作用wchar_t至 char16_t在 main() 的第一行.我用 -std=c+
以下代码按预期工作。源代码、文件“file.txt”和“out.txt”都是用utf8编码的。但是,当我在 main() 的第一行将 wchar_t 更改为 char16_t 时,它不起作用。我已经用
当我尝试运行它时: #include #include int _tmain(int argc, TCHAR *argv[]) { std::basic_ifstream file("Te
There are already questions在 Stackoverflow 上询问 为什么 basic_fstream不起作用。答案说char_traits仅专门用于 char和 wchar
上面写着“Apple Mach-O Linker (Id) Error "readIt(std::_1::basic_ifstream >&," 有没有人看到它一直卡住的错误?非常感谢! #inclu
我是一名优秀的程序员,十分优秀!