J
Julian
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:stringstream;
using std:stream;
using std:fstream;
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 ?
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:stringstream;
using std:stream;
using std:fstream;
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 ?