redirect cout to a file

Hello everyone

I am actually working on someone else's code. I have to work on the output generated by the two methods from that code. I am trying to redirect cout from two different functions to two different files. I have found one way of doing this on google, which is apparently simpler but it isnt working and is giving me some error messages. The files would be too big. Both of them would almost have 93,000 lines. Could you please tell me how do I modify it. I have tried streambuf but it didnt redirect all the cout to files. Could anyone please tell me how do I accomplish this task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ofstream myfile1;
myfile1.open (CME_fpath1.c_str());
     
myfile1 << RMV->func1(fpath, dateRun);
 
myfile1.close();
	 
//============================

ofstream myfile2;
myfile2.open (CME_fpath2.c_str());
	 
myfile2 << RMV->func2(fpath, dateRun);
	 
myfile2.close()


and the eroor message are

1
2
3
4
5
run_cme.cpp:39: error: no match foroperator<<’ in ‘myfile1 << BM->BreachMonitor::LoadAdapterData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& fpath)))))’
2	/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
3	/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
4	/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
5	/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/ostream:165: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]


There is a long list of similar error messages.
Thanks Bazzy.

I did use this example with rdbuf and it worked fine for smaller files but not for big file like which are having 93,000 lines each. Do I have to clear the buffer because I just used this example and I am not very familiar with streambuf/rdbuf.

Could you please guide.
What happened when you tried to redirect to those files?
Did they already exist ( already containing the 93000 lines ) or are the 93000 output by the functions?
Topic archived. No new replies allowed.