I would like to have output from my program to be written to cout as well as
a file. (actually, i want several other output options but this should
explain my problem in the simplest way). I have seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function calls that can output to
a number of ofstreams/ostringstreams etc. and implement the 'turning off/on'
by settting flags.
this is basically how my program works right now (for outputting to a SINGLE
stream at a time):
#define OUT cout
or
#define OUT (*outStream) // where outStream is a ostream to some file
....
OUT << "this is a test\n";
this is how i was planning on going about it :
deriving a class from ostringstream and overriding the operator << function.
the following sample code writes the 1st string to both cout as well as the
log file but...
1. it doesn't output the 2nd string to either file or cout.
2. and it crashes !! and i am not able to figure out why
#include <iostream>
#include <fstream>
#include <sstream>
using std::ostringstream;
using std::ostream;
using std::ofstream;
using std::cout;
class IOM : public ostringstream {
public:
IOM(){Log.open("log.txt");};
~IOM(){Log.close();};
friend IOM& operator<<(IOM& _O, const char *_X);
ofstream Log;
private:
};
IOM& operator<<(IOM& _O, const char *_X)
{
(ostringstream)_O << _X;
cout << _O.str();
_O.Log << _O.str();
return _O;
}
int main(int argc, char **argv)
{
IOM iom;
iom << "this is" << " a test";
return 0;
}
another question i had was the operator function written above handles only
char*. so, what if i had to pass an integer or even a user-defined class?
do i have to write override functions for every type ?
i am not hell bent on using this method. if someone knows of a better way to
implement this (that is, write to two ofstreams in one operator << function
call), please let me know...
also, on a somewhat different topic, i was able to change the above code to
#include <iostream.h> //as opposed to #include <iostream>
#include <fstream.h> //as opposed to #include <fstream>
but it did not accept
#include <sstream.h>
can someone explain why ?
and when i used just the:
#include <iostream.h>
#include <fstream.h>
i did not have to use the std:: scope resolver.
but when i used it along with
#include <sstream>
it came up with 'multiple declaration' problems
any someone explain this to me or point me to good site/link ? 2 5605
On Mon, 13 Dec 2004 13:35:51 -0600, "Julian" <ju****@nospamtamu.edu>
wrote: I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function calls that can output to a number of ofstreams/ostringstreams etc. and implement the 'turning off/on' by settting flags.
Here's how this is usually done. For simplification, I will assume for
now that the data you are outputting is passed as a std::string ... we
can handle the other types later.
1. Define an abstract base class (called, for example, "Logger") with
a pure virtual function, for example, writeLog(std::string const &).
2. Derive various concrete classes which implement the writeLog()
method. You can have a FileLogger, a DbLogger, and a ConsoleLogger,
for example. FileLogger writes to the file system, DbLogger does a
database update, and ConsoleLogger writes to stdout. You don't
necessarily need std::ostream for implementing the writeLog() function
-- it makes sense for FileLogger and ConsoleLogger, but probably not
for DbLogger. Therefore, I wouldn't inherit from ostream at all.
3. Define a class or subsystem in your application which manages the
output. This class, or subsystem, would keep a vector of pointers to
base class Logger. When it comes time to log your data, just iterate
through the vector and call the writeLog() function on your
polymorphic pointer.
this is basically how my program works right now (for outputting to a SINGLE stream at a time): #define OUT cout or #define OUT (*outStream) // where outStream is a ostream to some file ... OUT << "this is a test\n";
It is a very bad habit to use macros for this sort of thing. What you
REALLY want is an Abstract Factory (google for "design patterns").
this is how i was planning on going about it : deriving a class from ostringstream and overriding the operator << function. the following sample code writes the 1st string to both cout as well as the log file but... 1. it doesn't output the 2nd string to either file or cout. 2. and it crashes !! and i am not able to figure out why
#include <iostream> #include <fstream> #include <sstream>
using std::ostringstream; using std::ostream; using std::ofstream; using std::cout;
class IOM : public ostringstream { public: IOM(){Log.open("log.txt");}; ~IOM(){Log.close();}; friend IOM& operator<<(IOM& _O, const char *_X); ofstream Log; private: };
Do not use leading underscores in identifiers ... these are "reserved
for the implementation" (i.e. the compiler and all the standard
libraries).
IOM& operator<<(IOM& _O, const char *_X) { (ostringstream)_O << _X; cout << _O.str(); _O.Log << _O.str(); return _O; }
int main(int argc, char **argv) { IOM iom; iom << "this is" << " a test"; return 0; }
If you never use argc and argv, why include them at all?
another question i had was the operator function written above handles only char*. so, what if i had to pass an integer or even a user-defined class? do i have to write override functions for every type ?
Not necessarily, you could define a template class and log any type
you wish.
BTW it's overload, not override: overloading is when different
functions with the same name, often in the same class, take different
argument types and/or number of arguments, or have different
const-ness.
Overriding is writing two functions with the same name AND the same
arguments and number of arguments, one in a derived class which
inherits from a base class containing the other function which must be
declared as a virtual function in the base class.
i am not hell bent on using this method. if someone knows of a better way to implement this (that is, write to two ofstreams in one operator << function call), please let me know...
also, on a somewhat different topic, i was able to change the above code to #include <iostream.h> //as opposed to #include <iostream> #include <fstream.h> //as opposed to #include <fstream>
You shouldn't use these old headers (ending in *.h) which are there
only for backwards compatibility with code which uses them. It is
dangerous to mix the newer headers with the older ones exactly because
of problems such as the ones you describe below.
but it did not accept #include <sstream.h> can someone explain why ?
sstream is newer than the old headers which end with .h, so there
isn't one.
and when i used just the: #include <iostream.h> #include <fstream.h> i did not have to use the std:: scope resolver. but when i used it along with #include <sstream> it came up with 'multiple declaration' problems any someone explain this to me or point me to good site/link ?
Yes: http://www.parashift.com/c++-faq-lite/
--
Bob Hairgrove No**********@Home.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lutorm |
last post by:
Hi everyone,
I'm trying to use istream_iterators to read a file consisting of pairs
of numbers. To do this, I wrote the following:
#include <fstream>
#include <vector>
#include <iterator>
...
|
by: Karl |
last post by:
Hey everyone!
So I'm writing my own String class to wrap std::string and implement an
API as close to identical as possible to the Java API. It's pretty
small so far:
#include <string>
...
|
by: Ashwin |
last post by:
hi guys,
i have overloaded the << operator.as shown below.
ostream& operator<<(ostream &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks...
|
by: krzysztof.konopko |
last post by:
I cannot compile the code which defines a std::map type consisting of
built in types and operator<< overload for std::map::value_type.
See the code below - I attach a full example.
Note: if I...
|
by: subramanian100in |
last post by:
Consider the code:
#include <iostream>
using namespace std;
int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);
|
by: Stuart Golodetz |
last post by:
Hi guys,
I'm trying to making an instance of a templated operator<< for a
templated class a friend of that class (see below), to allow it to
access the class internals for output purposes.
...
|
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= |
last post by:
I'm having an error with this code
#include <iostream>
template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<< <>(std::ostream& os, const
TestOpTemplate<T>& m);...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |