C编程: error: expected ‘)’ before string constant

标签 c syntax

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/

相关文章:

c - 为什么 setjmp 传统上保存寄存器?

bash - 如果不被空格包围,为什么等于运算符不起作用?

c - 什么是 C 中的完整声明符?

php - PHP语法错误: Unexpected ']' [closed]

c++ - 没有标识符时如何读取复杂的 C++ 类型声明?

c - C 中函数返回的整数值

谁能给我解释一下这段代码?

c - scanf() 将换行符留在缓冲区中

c++ - C++ 和/或 C 中的 size_t 与 int

PHP SQL查询正确的语法