将一个variables传递给popen命令

我需要传递一个stringvariables到一个popen命令,我为了对一段encryption数据进行描述。 我需要使用的代码段是:

char a[]="Encrypted data"; popen("openssl aes-256-cbc -d -a -salt <a-which is the data i have to pass here>","r"); 

我应该怎么做才能将这个variables传递给命令。 我试着用:

 popen("openssl aes-256-cbc -d -a -salt %s",a,"r"); 

但编译显示错误,popen传递了太多的论据。 请帮忙。 提前致谢。 操作平台:Linux

使用snprintf构造传递给popen的命令字符串。

 FILE * proc; char command[70]; char a[]="Encrypted data"; int len; len = snprintf(command, sizeof(command), "openssl aes-256-cbc -d -a -salt %s",a); if (if len <= sizeof(command)) { proc = popen(command, "r"); } else { // command buffer too short } 

如果参数包含空格,引号或其他特殊字符,则使用snprintf构造命令字符串将会中断。

在Unix平台上,您应该使用pipe创建管道,然后使用posix_spawnp启动子posix_spawnp ,使用posix_spawnp将子进程的stdout连接到管道的输入端。