writing to output screen and file simultaneously

Hello everyone!

is there anyway I can write the output of a program both to the file and the output screen simultaneously? Code for writing to a file is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string fpath = "/usr/local/" + std::string("/Fouput_") + std::string(".log"); 

streambuf *psbuf, *backup;
fstream filestr1;
	
ifstream file(fpath.c_str());
if (!file)
{
	cout << " file does not exist - create one" << endl;

	filestr1.open (fpath.c_str());
				
	backup = cout.rdbuf();     // back up cout's streambuf
	psbuf = filestr1.rdbuf();   // get file's streambuf
	cout.rdbuf(psbuf);         // assign streambuf to cout
		 	
	Totals();
		     
	cout.rdbuf(backup);        // restore cout's original streambuf
	filestr1.close();
}     	
else
{
	cout << " read existing TRS file" << endl;
}
hi

You should leave cout as is and just use the commands like this

1
2
3
4

        filestr1 << data;
        cout << data;


The only other thing is that you might need to pass the fstream* to your Totals function.

Hope this helps
Shredded
Topic archived. No new replies allowed.