C++使用system()启动程序时没有得到所需的结果

C++ Launching programs with system() not giving desired results

本文关键字:结果 system 使用 程序 启动 C++      更新时间:2023-10-16

我有一个C++代码设置,它为我启动项目,我所要做的就是输入名称并启动。问题是,当我让C++应用程序启动它时,C++应用程序"变成了它"。我的意思是假装我正在使用它启动minecraft服务器。它会像minecraft服务器一样,不使用目录中的文件。如果有人能帮忙,请帮忙。

(这是代码)

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
    std::string textin;
    cin >> textin;
    if ( textin == "ATLauncher" ) {
        system("C:\users\USER\Main\minecraftserver\minecraftserver.exe");
        return 0;
    }
}

听起来你想在启动之前更改工作目录。在posix中工作,您想要的呼叫是chdir(path)。在系统调用之前进行该调用。

请注意,Windows将工作目录存储在其他位置(在快捷链接中),因此您可能需要查看该目录以确定正确的路径。