如何在C程序中使用参数执行可执行文件?

时间:2018-05-31 23:56:21

标签: c system

int s = system("./my_prog 32");有效,但如何将参数作为变量引入? int a = 32; int s = ("system %d", a);似乎不起作用(“函数'系统'的参数太多了。”

2 个答案:

答案 0 :(得分:2)

C中的system()函数采用类型为const char *的单个参数。这就是你的第一个例子有效的原因(但是,你的第二个例子是错误的)。

仍然可以使用sprintf()中的stdio.h功能来实现您的目标。 int a = 32; char command[80]; sprintf(command, "./my_prog %d", a); system(command);

答案 1 :(得分:1)

  

如何将参数作为变量引入?

一种常见的技术是使用import env class App(): def __init__(self): self.ser = MySerial() to_decorate = [] if env.test else ['myfunc'] for fn_name in to_decorate: fn = getattr(self, fn_name) setattr(self, fn_name, self.ser.decorator(fn)) 动态生成命令字符串。例如:

sprintf()