redirect stdout/stderr to a string
来源:互联网 发布:编程语言执行效率 编辑:程序博客网 时间:2024/04/30 03:43
Q:
there has been many previous questions about redirecting stdout/stderr to a file. is there a way to redirect stdout/stderr to a string?
A:
Yes, you can redirect it to an std::stringstream
:
std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); std::cout << "Bla" << std::endl; std::string text = buffer.str(); // text will now contain "Bla\n"
You can use a simple guard class to make sure the buffer is always reset:
struct cout_redirect { cout_redirect( std::streambuf * new_buffer ) : old( std::cout.rdbuf( new_buffer ) ) { } ~cout_redirect( ) { std::cout.rdbuf( old ); } private: std::streambuf * old; };
- redirect stdout/stderr to a string
- How To Redirect stderr To stdout ( redirect stderr to a File )
- dup2 redirect stdout stderr
- In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?
- nohup: redirecting stderr to stdout的解决办法
- nohup: redirecting stderr to stdout解决办法
- nohup: redirecting stderr to stdout解决办法
- [Cexpert-004] How to output strings to stdout/stderr directly?
- redirect stdout and back to screen
- 解决linux环境下nohup: redirecting stderr to stdout问题
- 重定向stdout & stderr
- stdin,stdout,stderr
- stderr,stdin,stdout
- stdin,stderr,stdout
- stdout和stderr
- stdout, stderr的区别
- stdin,stderr,stdout
- stdin,stdout,stderr转载
- i2c的读取cmd发送完毕后并不会立刻同步的读到数据
- 15个重要的Android代码
- QEMU编译问题
- 防止单例模式被攻击
- linux中rpm.bin文件与bin文件的区别?
- redirect stdout/stderr to a string
- C++中定义与声明
- C++读取txt文件
- 无堵塞的脚本加载
- BeerSong 这个程序的java 代码
- GetBuffer与ReleaseBuffer的用法,CString剖析
- TCP/IP学习笔记:路由与BGP
- ArcGIS API For Flex4+Papervision3d实现3D地图浏览
- C++能做什么