I have a program that executes various shell commands via system()
and occasionally prints to cout
. I want to redirect all output coming from system()
calls to a log file so they don't clutter up the normal output. Can I do this without having to append > log
to all my system commands?
看起来可以使用popen
关闭stdio文件描述符(0、1和2),然后在所需的任何输出设备上重新打开它们。
Using system
is just a bad idea, period. If you use fork
and execve
or posix_spawn
, you can easily make the necessary redirections and avoid all sorts of vulnerabilities from shell quoting issues.
If you can use a library that wrap process call. It is hard to code from posix. I use boost.process , it works fine. you can simply tell the lib how you want the output to be redirected...
my2c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.