我对 exec 有一个严重的问题。我已经尝试了 list(execl) 和 array(execv) 这两个选项,但问题仍然存在。我将给出我尝试拨打电话的函数。
#include <unistd.h>
#include <sys/types.h>
void MyFunc(string aparams[],char* infile,char* outfile,int k,int points){
int mcount=3;
char* offset= new char[5];
sprintf(offset,"%d",k);
char* pntr=new char[5];
sprintf(pntr,"%d",points);
char* *wparams=new char*[mcount];
for (int i = 0; i < mcount; i++) {
wparams[i] = new char[aparams[i].length() + 1];
strcpy(wparams[i], aparams[i].c_str());
}
char *cwd;
cwd=(char*)malloc(255);
getcwd(cwd,255);
strcat(cwd,"/");
strcat(cwd,wparams[0]);
cout << cwd << endl;
execl(cwd,wparams[0],"-i",infile,"-o",outfile,"-f",offset,"-n",pntr,"-a",wparams[1],wparams[2],wparams[3],(char*) NULL);
cout << "exec failed" << endl;
perror("The problem in exec is:");
exit(3);
}
aparams[0] 包含一个带有可执行文件名称的字符串,比如说“test”。我编译了 -> g++ test.cpp -o test -> 所以我得到了这个可执行文件。aparams 的其他位置包含测试程序的一些参数。
因此,测试永远不会运行(当我从命令行运行它时没问题)并且 perror 显示消息“exec 中的问题是:错误地址。”
我也尝试过强制转换所有参数(const char*),但没有任何改变。是争论的问题吗?还是可执行文件的问题?