Jos*_*eau 1 c++ string fork character exec
我试图在 C++ 中运行 execl(),但它失败了,我不知道为什么。
string fileName = "test.txt";
string computerName = "jssoile@palmetto.school.edu";
pid_t pid;
int status;
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) { /* for the child process: */
if (execl("scp", fileName.c_str(), computerName.c_str(), NULL) < 0) { /* execute the command */
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我的 execl() 调用失败并打印出来
*** ERROR: exec failed
Run Code Online (Sandbox Code Playgroud)
您正在使用exec需要完整路径作为第一个参数的变体。
代替:
execl( "scp", ... )
Run Code Online (Sandbox Code Playgroud)
与任一
execl("/usr/bin/scp", ...)
Run Code Online (Sandbox Code Playgroud)
或者
execlp("scp", ... )
Run Code Online (Sandbox Code Playgroud)
参考: http: //linux.die.net/man/3/execl