std::cout wapper
Posted: Fri Dec 07, 2012 3:20 pm
How would you make wrapper to out std::cout to text file in c++
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
#include <iostream> #include <fstream> void print(std::ostream & os) { os << 3.14159; } int main(int argc, char ** argv) { print(std::cout); std::ofstream ofs("somefile"); print(ofs); return 0; }I hope that helps.
I vote for using fsteams like bb wrote in his postdandymcgee wrote:Use rdbuf to redirect std::cout to a file:
http://www.cplusplus.com/reference/ios/ios/rdbuf/
This is exactly why I recommended rdbuf. Marauder and Falco are overlooking the fact that certain libraries assume std::cout INTERNALLY. I'm not sure that Lua does this, but I know for a fact that SDL does. Though you could try to change and recompile them, it is much easier to just forward the standard out stream to a log file.shawk wrote:But I need lua to print to it also so if lua print function only uses std::cout I would have to make a different function for lua to access the ofstream
Log debuglog("debug.log", true); debuglog << "Player triggered warp tile at (" << ev.x << "," << ev.y << ")" << std::endl;Of course, you probably shouldn't instantiate a log in a function that runs more than once, unless you change Log::open to use append mode.
Code: Select all
[2012-12-08 12:49:32] Player triggered warp tile at 73,22
1) Lua is not one of them. It is overridable. 2) SDL, a C library, uses cout, a function of the C++ standard library? Orly?dandymcgee wrote:This is exactly why I recommended rdbuf. Marauder and Falco are overlooking the fact that certain libraries assume std::cout INTERNALLY. I'm not sure that Lua does this, but I know for a fact that SDL does.shawk wrote:But I need lua to print to it also so if lua print function only uses std::cout I would have to make a different function for lua to access the ofstream
Actually, I seem to have remembered incorrectly. By default, SDL redirects stdout to stdout.txt in the working directory. You have to use some similar hack if you want stdout to go to a console window instead without having to recompile the library with NO_STDIO_REDIRECT set. So it's essentially the opposite of the OP's problem, my bad.Falco Girgis wrote:2) SDL, a C library, uses cout, a function of the C++ standard library? Orly?