cout on both screen and file

Defaultly cout will show on the console screen.
We can redirect the output to a file by change the streambuf.

But, sometimes we would like to see the message and message and keep them in log file as well. Can we make the cout put on both screen and file using the same line:
cout << val

Thanks.
Yes! some long codes can be reduced in one line only (lol). same line but too long.

going back to your question. Why would you want to make it in one line only?

1
2
3
4
5
6
7
8
9
ifstream inFile("input.txt");
ofstream outFile(“output.txt”);
inFile.tie(&outFile);

outFile << "Hello there!";//here
cout <<val;
//one line
outFile << "Hello there!";cout <<val;//omg <-- it's not clean


EDIT: make code more efficient, cleaner and easier to understand.
Last edited on
Sorry that I didn't say my question clearly.

I want to put val 'm' to both screen and file. I can do it by:

1
2
3
4
5
6
int m = 10;
ofstream outFile("output.txt");

outFile << m; // put on file
cout << m; // put on screen
//- using this conventional method, two puts operations are conducted separately. 


Can I do some streambuf configures, then:
1
2
3
4
5
6
7
int m = 10;
ofstream outFile("output.txt");

//- ? some configuration on streambuf of outFile and cout

cout << m; // put on screen, and screen
//- then this single operation can achieve the same results as previous code. 

Last edited on
Hmm...

did you try operator overloading??

overload the cout?...
Thanks for your reply.

How can I do the overload if their the arguments are the same, but operations are different.

I got a idea about to redefine a class (STRA), and operates on cout and outFile, the problems are:
1. There are so many overload options for insert operator << , I haven't understood all the options.
2. Then I should replacing cout with sout (STRA sout), however, all my existing code are using cout.

So I prefer some simple configurations rather than modify most of my codes.

Thx.
Hmm... nevermind using operator<<

I'm not an expert, so I think your problem is a challenge for experts..

Do you really need to make it in a single line?

the insertion operator for ofstream is different from the insertion operator of cout...

Let's hope an expert would post in your thread and solve your problem.. Goodluck
There are thousands of cout lines, so there will be mass of work if I have to add a fstream line.
Hope there will be a better solution.

"The insertion operator for ofstream is different from the insertion operator of cout..."
But they do have common streambuf.
If insertion can be broadcasted to multiple streambuf, then my problem can be solved :)

Thanks for your warm reply.
Last edited on
I got a idea about to redefine a class (STRA), and operates on cout and outFile, the problems are:
1. There are so many overload options for insert operator << , I haven't understood all the options.
2. Then I should replacing cout with sout (STRA sout), however, all my existing code are using cout.
IMO It's a good idea you can solve these problems:
1. Use a single template << so it will work with all the existing types supporting cout <<
2. #define cout sout so the preprocessor will modify that for you. ( If you have std::cout you can extend the std namespace to contain sout )
1. Use a single template << so it will work with all the existing types supporting cout <<
Would you please give me an example regarding the template operator.
Thanks.
Last edited on
I got some material from
http://www.codeproject.com/KB/cpp/functemplates.aspx

It almost fixed my problem, except 'endl'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class mstream
{
  public:
  ofstream coss;
  mstream(void);
  ~mstream(void);
};

template <class T>
mstream& operator<< (mstream& st, T val)
{
  st.coss << val;
  cout << val;
  return st;
};

//Works well with
 sout << m << 0.1  << " haha\n";
//- But doesn't work well for
 sout << endl;

Last edited on
hmm...

Good for you..
try using
 
sout<<"\n";

or maybe
 
sout<< flush;//?? 


EDIT: endl is a manipulator, I think you must revised your template declaration and include endl there.
Last edited on
sout << "\n" works :)

And for "endl", which is a function template, the following code should be added.
1
2
3
4
5
6
  mstream& operator<< (ostream& (*pfun)(ostream&))
  {
    pfun(coss);
    pfun(cout);
    return *this;
  }


So, all fine now. Thank you guys.
Topic archived. No new replies allowed.