我写了以下内容:
#include <stdlib.h>
#include <stdio.h>
void ExecAsRoot (char* str);
int main ()
{
printf ("Host real ip is:");
ExecAsRoot("ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'");
return 0;
}
void ExecAsRoot (char* str) {
system (str);
}
我的期望输出是:
Host real ip is:7.17.11.29
实际输出结果为:
7.17.11.29
Host real ip is:
为什么会这样?
setbuf
的调用放在一个函数中,最好是在main
函数的开头。 - dbushsetvbuf(stdout, NULL, _IONBF, 0);
进行了工作。 - Joel G Mathewfflush
。 - phuclv