470,596 Members | 1,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Inheriting from fstream

Hi,

My objective is to add enable & disable functionality to
the ofstream class. I want to have a log file where I
can disable and enable output to it. When my first
tests pass, I want to disable the annotations written
to the log file, but enable the text from the newer
test cases.

I've searched the C++ newsgroups and the FAQ-Lite
and didn't find anything relevant.

"The C++ Standard Library", by Josuttis, shows how
to modify an ostream class. I've successfully implemented
the enable/disable functions using a streambuf and
attached it to an ostream class.

I've also looked at Dietmar Kuhl's site and his
site explains how to modify an ostream but not an
ofstream.

However, I would like to add the functionality to
the file stream. The ofstream class does not provide
a constructor with a streambuf parameter (at least
Borland's compiler doesn't).

In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?

#include <iostream>
#include <fstream>
#include <streambuf>
#include <cstdlib>
using namespace std;

class logfile
: public streambuf
{
public:
logfile()
: enabled(true)
{logstream.open("log.txt");}
bool enabled;
ofstream logstream;

protected:
virtual int_type overflow (int_type c)
{
if (enabled && (c != EOF))
{
logstream << c;
}
return c;
}
virtual streamsize xsputn(const char * s, streamsize num)
{
if (enabled)
{
logstream.write(s, num);
}
return num;
}
};
int main(void)
{
logfile lf;
ostream out(&lf);
out << "This should be printed.\n";
lf.enabled = false;
out << "This should not be printed.\n";
lf.enabled = true;
out << "After re-enabling.\n";

return EXIT_SUCCESS;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #1
4 2809

"Thomas Matthews" <Th**************************@sbcglobal.net> wrote in
message news:uv**************@newssvr32.news.prodigy.com.. .
Hi,

My objective is to add enable & disable functionality to
the ofstream class. I want to have a log file where I
can disable and enable output to it. When my first
tests pass, I want to disable the annotations written
to the log file, but enable the text from the newer
test cases.

I've searched the C++ newsgroups and the FAQ-Lite
and didn't find anything relevant.

"The C++ Standard Library", by Josuttis, shows how
to modify an ostream class. I've successfully implemented
the enable/disable functions using a streambuf and
attached it to an ostream class.

I've also looked at Dietmar Kuhl's site and his
site explains how to modify an ostream but not an
ofstream.

However, I would like to add the functionality to
the file stream. The ofstream class does not provide
a constructor with a streambuf parameter (at least
Borland's compiler doesn't).

In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?


Have you considered inheriting from filebuf (which is fstream's stream
buffer class) and then attaching your filebuf derived object to an ostream?

john
Jul 22 '05 #2
"Thomas Matthews" <Th**************************@sbcglobal.net> wrote in
message news:uv**************@newssvr32.news.prodigy.com.. .
My objective is to add enable & disable functionality to
the ofstream class. I want to have a log file where I
can disable and enable output to it. When my first
tests pass, I want to disable the annotations written
to the log file, but enable the text from the newer
test cases.


IIRC, disabling the output of a stream can be as simple
as setting an error flag ( using ios::clear() ) or
setting the stream buffer pointer it stores to NULL
( using rdbuf(NULL) ).

I do not remember the details of this, but it should
be simple enough and will not require any subclassing.
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #3
John Harrison wrote:
"Thomas Matthews" <Th**************************@sbcglobal.net> wrote in
message news:uv**************@newssvr32.news.prodigy.com.. .

[snip]
In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?

Have you considered inheriting from filebuf (which is fstream's stream
buffer class) and then attaching your filebuf derived object to an ostream?

john


This works, but one has to use the "open" method of the
filebuf before writing begins; versus the constructor
of ofstream or fstream having the filename and mode
in the constructor.

Thanks for the suggestion.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:10*************@newssvr32.news.prodigy.com...
John Harrison wrote:
"Thomas Matthews" <Th**************************@sbcglobal.net> wrote in
message news:uv**************@newssvr32.news.prodigy.com.. .

[snip]
In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?

Have you considered inheriting from filebuf (which is fstream's stream
buffer class) and then attaching your filebuf derived object to an ostream?
john


This works, but one has to use the "open" method of the
filebuf before writing begins; versus the constructor
of ofstream or fstream having the filename and mode
in the constructor.

Thanks for the suggestion.


Yes, but you could wrap it all up in a class.

class LogBuffer : public filebuf
{
...
};

class LogStream : public ostream
{
public:
LogStream(const char*, int mode)
{
...
}
private:
LogBuffer buffer;
};

john
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Briggs | last post: by
3 posts views Thread by David Blasdell | last post: by
9 posts views Thread by Someonekicked | last post: by
1 post views Thread by MForey | last post: by
5 posts views Thread by ehui928 | last post: by
6 posts views Thread by wiso | last post: by
5 posts views Thread by neowillis | last post: by
6 posts views Thread by Gaijinco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.