【发布时间】:2011-01-13 06:51:16
【问题描述】:
通过 xcode 运行以下代码时,我得到不一致的行为。有时它会正确打印 git 版本,有时它不会打印任何东西。但是,shell 命令的返回码始终为 0。关于为什么会这样的任何想法?我做错了什么?
#define BUFFER_SIZE 256
int main (int argc, const char * argv[])
{
FILE *fpipe;
char *command="/opt/local/bin/git --version";
char line[BUFFER_SIZE];
if ( !(fpipe = (FILE*)popen(command, "r")) )
{ // If fpipe is NULL
perror("Problems with pipe");
exit(1);
}
while ( fgets( line, sizeof(char) * BUFFER_SIZE, fpipe))
{
// Inconsistent (happens sometimes)
printf("READING LINE");
printf("%s", line);
}
int status = pclose(fpipe);
if (status != 0)
{
// Never happens
printf("Strange error code: %d", status);
}
return 0;
}
【问题讨论】: