473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 3033

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.