将f打印到控制台窗口和文件
printf to both console window and file?
我有很多用Visual Studio 2005编译的C++程序。它们大多是在控制台窗口中运行的小型服务器模块。无论如何,我遇到的问题是,文本只能显示在控制台窗口或日志文件中,但不能同时显示。每个程序都有一个命令行选项来指定日志文件。以下是我调用的函数,用于将stdout和stderr重定向到一个文件。
void consoleobj::setstdouterr(const stringobj& printstr)
{
#if !defined(_WIN32_WCE)
freopen(printstr.c_str(),"w",stdout);
#ifdef _MSC_VER
::SetStdHandle(STD_ERROR_HANDLE,GetStdHandle(STD_OUTPUT_HANDLE));
#endif
#endif
// make log msgs flush to log file(cout does this(on n?), printf doesn't)
//now if both redir to same log file, msgs should be in right order
setvbuf(stdout, NULL, _IONBF, 0); //no buffering
setvbuf(stderr, NULL, _IONBF, 0); //no buffering
}//end method setstdouterr
有没有什么方法可以设置stdout和stderr同时写入控制台窗口和可选日志文件的?我见过重定向cout或包装函数的代码,但我们的print语句都使用printf,如果可能的话,我更喜欢使用类似于consoleobj库中的函数来设置它。谢谢
而不是在代码中实现此功能
您可以在Unix中使用众所周知的实用程序tee
它有一个Windows版本,名为wtee.exe
。
C:> programm | wtee log.txt
相关文章:
- 如何在cpp文件之间切换窗口?在Qt中
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 在 opengl 中渲染 obj 文件时出现黑窗口
- 非 ASCII 文件路径窗口
- 'generate'不是窗口头文件中'std::random_device'的成员<random>
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- 我需要通过窗口句柄(HWND)获取文件,我该怎么办?
- 如何在窗口中显示文件夹中所有文件的名称?Qt c++.
- 尝试使用 CPP 在一个 Qt 窗口中显示多个 png 文件
- 将 c++ 程序语法错误重定向到窗口中的文件
- 如何将 QML 文件分配给主窗口/应用程序
- 如何在窗口上的命令行中读取输入 .in 文件
- Visual C 关闭文本文件和打开新的控制台窗口
- 窗口中的文件损坏 电源故障后操作系统
- 如何在C Winapi中获取活动文件Explorer窗口的路径
- C++文件选择窗口
- 尝试使用窗口注册表中的路径和 DeleteFile() 方法删除.exe文件
- 调整窗口内存映射文件的大小,而不会使指针失效
- 将文件从一个位置移至窗口C 的另一个位置