Is there anyway to get the command stdout and stderr output before it ends running? I've been trying popen
and here's the code:
string exec(const char* cmd) {
char buffer[128];
string result;
string modCmd = (string)cmd + (string)" 2>&1";
FILE* pipe = popen(modCmd.c_str(), "r");
if (!pipe) throw runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr)
{
result += buffer;
cout << buffer;
}
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
but the problem is, all the cout
s stack up and run after the command's running is over.
PS The command I'm trying to execute is aria2c
如@VTT所述,问题是aria2c不会刷新其输出,但是有一种解决方法是运行stdbuf -o0 aria2c
而不是运行aria2c ...
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.