shell_command(char gcommand[100]) {
char output[100];
system(gcommand ">" output);
return output;
}
给我
错误:字符串常量之前应有“)”
我不太清楚为什么会发生这种情况。感谢任何帮助:)
最佳答案
字符串文字可以像这样连接,但字符串值不能。
此外,您似乎希望 gcommand
的输出最终出现在缓冲区 output
中。
使用system
函数不可能做到这一点。假设您要在 POSIX 风格的 shell 中执行,其中 >
是 shell 重定向运算符,它右侧的内容必须是文件名(或描述符)在外壳中。
要执行命令并捕获输出,一种方法是使用 POSIX popen功能:
FILE *pipe = popen(gcommand, "r");
char output[100] = { 0 };
if ( pipe )
{
fgets(output, sizeof output, pipe);
pclose(pipe);
}
return output;
关于C编程: error: expected ‘)’ before string constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25297516/