编程忽略Cout [英] Programatically Ignore Cout

查看:124
本文介绍了编程忽略Cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否有一个技巧来切换所有 cout<< 函数不打印出可见输出?我试图把一些由我和其他人编写的代码加入到一起来演示。我不想将输出重定向到一个文件,并希望一个解决方案,在Windows和Linux之间有一定程度的兼容性。

Does anybody know if there is a trick to toggle all the cout << functions to not print out visible output? I am trying to hack together some code written by me and some other people to put together a demo. I would rather not redirect the output to a file and would like a solution that had some measure of compatibility between Windows and Linux.

在我的方案中,我有很多行代码与各种 #defines 控制某些方法产生调试输出时。我想调用类似:

In my scenario I have many many lines of code with with various #defines controlling when certain methods produce debug output. I want to call something like:

cout.off();
driverForAffectA();
driverForAffectB();
cout.on();
printSpecializedDebug();
exit(0);


推荐答案

您可以更改cout的流缓冲区。

You can change cout's stream buffer.

streambuf *old = cout.rdbuf();
cout.rdbuf(0);
cout << "Hidden text!\n";
cout.rdbuf(old);
cout << "Visible text!\n";

编辑:

'注释你可以缩短代码一点:

Thanks to John Flatness' comment you can shorten the code a bit:

streambuf *old = cout.rdbuf(0);
cout << "Hidden text!\n";
cout.rdbuf(old);
cout << "Visible text!\n";

这篇关于编程忽略Cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆