Can I output contents into multiple ostreams?

Like these:

1
2
3
4
ofstream ofs1("test1.dat");
ofstream ofs2("test2.dat");

cout<<ofs1<<ofs2<<"Hello, Will I show up in all three terminals?"<<endl;


Looks like the above syntax won't get what I want, then how can I get what I want? Thanks.
Why don't you just repeat the instruction?

1
2
ofs1 << "Hello" << endl;
ofs2 << "Hello" << endl;
I just don't want to repeat the content, the content sometimes is a lot.
Then you can have a vector of ofstreams and perform the "writing" inside a loop.
That should do!!! Thanks, ne555! Thanks, minomic!
Another simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

int main(int argc, char const *argv[])
{
	int N = 2;
	std::ofstream streams[N];

	streams[0].open ("test1.dat");
	streams[1].open ("test2.dat");

	for(int i = 0; i < N; ++i) {
		streams[i] << "Content" << std::endl;
	}

	for(int i = 0; i < N; ++i) {
		streams[i].close();
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.