Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用std::system启动进程时,我可以指定一个工作目录吗?_C++_Process - Fatal编程技术网

C++ 使用std::system启动进程时,我可以指定一个工作目录吗?

C++ 使用std::system启动进程时,我可以指定一个工作目录吗?,c++,process,C++,Process,我想启动一个完整路径的可执行文件: std::system(“C:/binary.exe”) 有没有办法在这种情况下指定一个工作目录? 我不相信有一种可移植的方式来实现你想要的,至少它不是由我所能告诉的C++标准强制的。一般来说,如果您需要比system()提供的功能更多的功能,那么您应该去别处看看。在Linux和Unix系统上,这将是和函数。在窗户上 实现这一点的Linux方法的未经测试的代码是: #include <cstdio> #include <unistd.h>

我想启动一个完整路径的可执行文件:

std::system(“C:/binary.exe”)


有没有办法在这种情况下指定一个工作目录?

我不相信有一种可移植的方式来实现你想要的,至少它不是由我所能告诉的C++标准强制的。一般来说,如果您需要比
system()
提供的功能更多的功能,那么您应该去别处看看。在Linux和Unix系统上,这将是和函数。在窗户上

实现这一点的Linux方法的未经测试的代码是:

#include <cstdio>
#include <unistd.h>

int
main()
{
    const pid_t pid( fork() );
    if ( !pid ) {
        // child process
        if ( chdir("/tmp") ) {
            perror( "chdir" );
        }
        execl( "/binary", "binary", (char*)0 );
        perror( "execl(\"/binary\")" );
        _exit( 1 );
    }
}
#包括
#包括
int
main()
{
常数pid_t pid(fork());
如果(!pid){
//子进程
if(chdir(“/tmp”)){
perror(“chdir”);
}
execl(“/binary”,“binary”,“char*)0);
perror(“execl(\”/binary\”);
_出口(1);
}
}

我不相信有一种可移植的方式来实现你想要的,至少它不是由我所能告诉的C++标准规定的。一般来说,如果您需要比
system()
提供的功能更多的功能,那么您应该去别处看看。在Linux和Unix系统上,这将是和函数。在窗户上

实现这一点的Linux方法的未经测试的代码是:

#include <cstdio>
#include <unistd.h>

int
main()
{
    const pid_t pid( fork() );
    if ( !pid ) {
        // child process
        if ( chdir("/tmp") ) {
            perror( "chdir" );
        }
        execl( "/binary", "binary", (char*)0 );
        perror( "execl(\"/binary\")" );
        _exit( 1 );
    }
}
#包括
#包括
int
main()
{
常数pid_t pid(fork());
如果(!pid){
//子进程
if(chdir(“/tmp”)){
perror(“chdir”);
}
execl(“/binary”,“binary”,“char*)0);
perror(“execl(\”/binary\”);
_出口(1);
}
}