[英]How to send command to Linux command from C program
我正在尝试从C程序向Linux命令行发送命令,但我不确定其中一部分是怎么做的。
例如在我的C代码中
system("raspistill -o image.jpg");
我想做的是在“图像”的末尾添加一个数字,并在每次程序运行时将其递增,但是我如何将变量n
传递给仅寻找一个变量的system()
函数呢? const char
?
我试过了,但是没有用:
char fileName = ("raspistill -o image%d.jpg",n);
system(filename);
我已经尝试过搜索,但是还没有找到有关如何向其中添加变量的任何信息。 对不起,菜鸟问题。
char fileName[80];
sprintf(fileName, "raspistill -o image%d.jpg",n);
system(filename);
首先,一个String是一个char数组,所以要声明(我想你只是想强调一下):
char command[32];
因此,简单的解决方案将是:
sprintf(command, "raspistill -o image%d.jpg", n);
然后调用system(command);
。 这正是您所需要的。
编辑:
如果 需要程序输出 ,请尝试popen
:
char command[32];
char data[1024];
sprintf(command, "raspistill -o image%d.jpg", n);
//Open the process with given 'command' for reading
FILE* file = popen(command, "r");
// do something with program output.
while (fgets(data, sizeof(data)-1, file) != NULL) {
printf("%s", data);
}
pclose(file);
资料来源: C:运行系统命令并获取输出?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.