【发布时间】:2016-05-08 04:23:22
【问题描述】:
有人知道我该如何解决这个问题吗?
char bash_cmd[256] = "curl";
char buffer[1000];
FILE *pipe;
int len;
pipe = popen(bash_cmd, "r");
if (NULL == pipe) {
perror("pipe");
exit(1);
}
fgets(buffer, sizeof(buffer), pipe);
printf("OUTPUT: %s", buffer);
pclose(pipe);
以上代码片段返回以下内容:
OUTPUT: (�3B
而不是它应该返回的是:
curl: try 'curl --help' or 'curl --manual' for more information
出了点问题,我不知道是什么。当我将“curl”替换为“ls -la”时,它可以正常工作,但无论出于何种原因,只有当我使用 curl 时,它才会正确地将输出保存到缓冲区中。我能做些什么来解决这个问题?提前谢谢
此外,将“curl”替换为 curl 的完整路径 (/usr/bin/curl) 也不起作用。 ;(
【问题讨论】:
-
首先,您未能检查函数的返回值。