如何覆盖std :: filebuf? [英] how do I override a std::filebuf?

查看:100
本文介绍了如何覆盖std :: filebuf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用STLPort 5.2.1的Visual Studio 2008 C ++ 03应用程序,我想在其中使用自定义的std::filebuf实现.例如:

I have a Visual Studio 2008 C++ 03 application using STLPort 5.2.1 where I would like to use a custom std::filebuf implementation. For example:

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的成员.如果我单步执行代码,就会看到<<运算符转到

Unfortunately, none of the members of MyFileBuf are ever called. If I step through the code, I see that the << operator goes to

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()

我希望调用堆栈的顶部位于:

where I would expect the top of the callstack to be:

MyFileBuf::xsputn(const char* p, long int n)

但是,文件已正确写入.有人可以帮助我了解我要去哪里哪里吗?

The files are, however, written correctly. Can anybody help me understand where I am going wrong?

推荐答案

感谢@jahhaj和@DanielKO的帮助.

Thanks @jahhaj and @DanielKO for your help.

解决方案似乎是这样的:

The solution appears to be something like this:

#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 :: filebuf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆