如何覆盖std::filebuf
how do I override a std::filebuf?
我有一个使用STLPort 5.2.1的Visual Studio 2008 C++03应用程序,我想在其中使用自定义std::filebuf
实现。例如:
class MyFileBuf : public std::filebuf
{
protected:
virtual int_type sync()
{
// breakpoint here never fires
return std::filebuf::sync();
};
virtual std::streamsize xsputn( const char_type* p, std::streamsize n )
{
// breakpoint here never fires
return std::filebuf::xsputn( p, n );
};
virtual int_type overflow( int_type c = traits_type::eof() )
{
// breakpoint here never fires
return std::filebuf::overflow( c );
};
};
class MyFileStream : public std::ofstream
{
public:
MyFileStream() : std::ofstream( new MyFileBuf() ) { clear(); };
~MyFileStream() { delete rdbuf(); };
};
int main()
{
MyFileStream fs;
fs.open( "test.txt" );
fs << "this is a test" << std::endl;
return 0;
}
不幸的是,MyFileBuf的任何成员都没有被调用。如果我遍历代码,我会看到<<
操作符转到
stlpd_std::basic_streambuf<char,stlpd_std::char_traits<char> >::xsputn(const char* __s, long int __n)
stlpd_std::basic_streambuf<char,stlpd_std::char_traits<char> >::sputn(const char* __s, long int __n)
stlpd_std::basic_ostream<char,stlpd_std::char_traits<char> >::_M_put_nowiden(const char* __s)
stlpd_std::operator<<<stlpd_std::char_traits<char> >(stlpd_std::basic_ostream<char,stlpd_std::char_traits<char> >& , const char* __s )
main()
我希望调用堆栈的顶部是:
MyFileBuf::xsputn(const char* p, long int n)
然而,这些文件写得是正确的。有人能帮我理解我哪里错了吗?
感谢@jahhaj和@DanielKO的帮助。
解决方案似乎是这样的:
#include <iostream>
#include <fstream>
using namespace std;
class MyFileBuf : public std::filebuf
{
protected:
virtual int_type sync()
{
return std::filebuf::sync();
};
virtual std::streamsize xsputn( const char_type* p, std::streamsize n )
{
return std::filebuf::xsputn( p, n );
};
virtual int_type overflow( int_type c = traits_type::eof() )
{
return std::filebuf::overflow( c );
};
};
class MyFileStream : public std::ostream
{
public:
MyFileStream() : std::ostream( 0 ) { init( &buf_ ); };
MyFileStream( const char* filename, std::ios_base::openmode mode = std::ios_base::out )
: std::ostream( 0 )
{
init( &buf_ );
this->open( filename, mode );
}
bool is_open() const { return buf_.is_open(); };
void close() { buf_.close(); };
void open( const char* filename, std::ios_base::openmode mode = std::ios_base::out )
{
buf_.open( filename, mode );
};
std::filebuf* rdbuf() { return &buf_; };
private:
MyFileBuf buf_;
};
int main()
{
MyFileStream fs( "test.txt" );
fs << "this is a test" << std::endl;
return 0;
}
示例
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 如何仅使用 std::filebuf 将数据(二进制模式)写入文件
- 文件大小调整未使用std::filebuf反映
- 如何覆盖std::filebuf
- 将 std::filebuf 限制为(存档)文件内的文件
- 为什么在Windows上std::fopen和std::filebuf::open会裁剪尾随空格字符
- 什么是 std::filebuf 等价于 ifstream::eof ::fail 和 ::tellg