Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Extending ofstream class?

Reply
Thread Tools

Extending ofstream class?

 
 
Lars Yencken
Guest
Posts: n/a
 
      08-14-2003
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: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:ut );
}

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-14-2003

"Lars Yencken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Lars Yencken
Guest
Posts: n/a
 
      08-14-2003
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
 
Reply With Quote
 
Zaphod Zaphod is offline
Junior Member
Join Date: Mar 2008
Posts: 2
 
      05-22-2013
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;
    }
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
std::ofstream filename red floyd C++ 1 08-14-2003 07:59 PM
File cannot open with ofstream constructor ! tornado C++ 1 07-29-2003 04:38 PM
help: ofstream doesn't always create files? james545@my-deja.com C++ 0 07-16-2003 02:27 PM
writes to files passed as struct of ofstream objects fail m vaughn C++ 0 07-11-2003 01:06 AM



Advertisments