L
Lars Yencken
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 );
}
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:
{
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:
}