使用包含给定空格的路径启动程序

时间:2016-06-21 15:56:02

标签: c++

我有一个命令字符串,例如: 开始C:\Users\...\test application.exe 有一个有空格的路径 现在我想使用system()函数启动它:

system(command.c_str());

但问题是,它不会启动,因为路径包含空格。 我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:5)

  

我该怎么做才能解决这个问题?

你需要知道的第一件事是system()使用shell来执行命令,如果路径包含空格,shell希望你用""括起程序路径。 / p>

使用当前的C ++标准,最简单的解决方法是使用raw string literal

std::string command = R"("C:\Users\test application.exe")";
system(command.c_str());

否则(对于较旧的C ++标准),您需要转义所有特殊字符:

std::string command = "(\"C:\\Users\\test application.exe\")";
                     // ^   ^      ^                     ^
相关问题