Extending ofstream class?

Discussion in 'C++' started by Lars Yencken, Aug 14, 2003.

  1. Lars Yencken

    Lars Yencken Guest

    Hello,

    I'm working on a project where we need to index files as we're writing them,
    which basically just involves determining how many bytes are output each
    time we write to a stream.

    What I've been trying is essentially the code below. It seg. faults though
    when I try it out. There are some obvious deficiencies to me:
    - I shouldn't have to have a private ofstream attribute, I should be able to
    use the inherited constructor and methods to do the same job.
    - It seems like sometimes it uses the template << operator that I defined,
    and at other times it uses the inherited operator, though I'm not sure why.

    Has anyone tried to do something like this before, or can someone please
    give me some advice as to what I'm doing wrong?

    Thanks,
    Lars

    IndexedStream.h
    --------------------------------------------------------------------------

    #include <fstream>
    #include <string>

    using namespace std;

    class IndexedStream : public ofstream
    {
    public:
    IndexedStream( string filename );

    template< class T >
    IndexedStream& operator<<( const T& value );

    private:
    ofstream m_outfile;
    int m_offset;
    }

    template< class T >
    IndexedStream& IndexedStream::eek:perator<<( const T& value )
    {
    m_outfile << value;
    doSomethingWith( (int) m_outFile.tellp() - m_offset );
    }


    IndexedStream.cpp
    ---------------------------------------------------------------------------

    #include "IndexedStream.h"

    IndexedStream::IndexedStream( string filename )
    {
    m_outfile.open( filename.c_str(), ios::eek:ut );
    }
    Lars Yencken, Aug 14, 2003
    #1
    1. Advertising

  2. "Lars Yencken" <> wrote in message
    news:...
    > Hello,
    >
    > I'm working on a project where we need to index files as we're writing

    them,
    > which basically just involves determining how many bytes are output each
    > time we write to a stream.
    >
    > What I've been trying is essentially the code below. It seg. faults though
    > when I try it out. There are some obvious deficiencies to me:
    > - I shouldn't have to have a private ofstream attribute, I should be able

    to
    > use the inherited constructor and methods to do the same job.


    I disagree, you need a private attribute, in this case inheriting is wrong.

    > - It seems like sometimes it uses the template << operator that I defined,
    > and at other times it uses the inherited operator, though I'm not sure

    why.

    Right, operator<< is not virtual, so this method is never going to work
    reliably.

    >
    > Has anyone tried to do something like this before, or can someone please
    > give me some advice as to what I'm doing wrong?
    >


    You should not try to force fit your indexed scheme into the usual iostream,
    its too different . This means you should not inherit from ofstream.

    When make this change it should all become a lot easier.

    john
    John Harrison, Aug 14, 2003
    #2
    1. Advertising

  3. Lars Yencken

    Lars Yencken Guest

    John Harrison wrote:
    > You should not try to force fit your indexed scheme into the usual
    > iostream, its too different . This means you should not inherit from
    > ofstream.
    >
    > When make this change it should all become a lot easier.
    >
    > john


    Thanks John. I've tried it out and it's actually working nicely. The main
    annoyance with this method is that I can't seem to write endl to my class;
    I get compile errors when I do. It can easily be worked around by writing
    just the string for newline, but it'd be nice if there was a way to do this
    too, especially since it'd prevent me having to modify a lot of other code.
    I'm guessing that if writing endl doesn't work, stream modifiers in general
    won't work either.

    Any suggestions on this?

    Lars
    Lars Yencken, Aug 15, 2003
    #3
  4. Lars Yencken

    Zaphod

    Joined:
    Mar 18, 2008
    Messages:
    1
    To answer your question (from 2003) but might help someone else, you need to add something like this so your code above can handle processing the endl to your << operator:

    Code:
        // this is the type of std::cout
        typedef std::basic_ostream< char, std::char_traits<char> > CoutType;
    
        // this is the function signature of std::endl
        typedef CoutType & (*StandardEndLine)(CoutType&);
    
        // define an operator<< to take in std::endl
        IndexedStream& operator << (StandardEndLine manip) {
            m_outfile  << endl;
            return *this;
        }
    Zaphod, May 23, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armando
    Replies:
    2
    Views:
    7,033
    Martijn Lievaart
    Jan 23, 2004
  2. Simon Elliott
    Replies:
    0
    Views:
    365
    Simon Elliott
    Jan 11, 2005
  3. Matthias Kaeppler
    Replies:
    1
    Views:
    322
    Kanenas
    Apr 22, 2005
  4. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    587
    Ivan Johansen
    Jul 14, 2005
  5. Replies:
    2
    Views:
    418
Loading...

Share This Page