
[英]Why if I input EOF from keyboard Clion don't print on Run window the output of the program?
[英]Don't print the output of system call
我正在尝试执行超时的 wget,并且程序不断显示所有系统调用以检查 wget 是否仍在运行(为此我正在使用 pgrep),有什么方法可以不显示 pgrep 调用的结果吗?
std::string systemCall{};
systemCall = "wget " + downloadLink + " &";
system(systemCall.c_str());
time_t timer_old, timer_current;
time(&timer_old);
int activeWget = 0;
do
{
activeWget = system("pgrep wget"); //always print the process id of wget
} while ((activeWget == 0) && (difftime(timer_current, timer_old) < 3));
如果你真的想继续使用system()
,那么只需将 pipe 和 output 到 /dev/null:
system("mycall 2>&1 >/dev/null");
一个更好的方法来实现这一点,将使用这样的东西:
string exec(const string& cmd, int32_t& returnCode) {
char outputBuffer[512];
string commandOutput;
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
// you'd have to throw a system_error or something here; or just remove the exception
throw getSysError(errno, "Failed to start process!");
try {
while (!feof(pipe) && fgets(outputBuffer, sizeof(outputBuffer) / sizeof(outputBuffer[0]), pipe) != NULL) {
commandOutput += outputBuffer;
}
} catch (...) {
returnCode = pclose(pipe);
throw;
}
returnCode = pclose(pipe);
return commandOutput;
}
void foo() {
int32_t returnCode = 0;
const string exe = "mycmd -poption";
exec(exe, returnCode); // and just ignore the return value
}
我会注意到上面有些遗留代码,但我已经成功使用了一段时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.