开发者

How to deal with spaces within paths when using the system()?

开发者 https://www.devze.com 2023-03-08 05:35 出处:网络
I am still new to C++ and am working out a way to open a program within my C++ program. The problem is that whenever I have spaces in my paths, it sees it as different parameters.

I am still new to C++ and am working out a way to open a program within my C++ program. The problem is that whenever I have spaces in my paths, it sees it as different parameters.

int _tmain(int argc, _TCHAR* argv[])
{
    system("C:\\Users\\blah\\Desktop\\a\\ b.txt");
    return 0;
}

The output I receive is:

'C:\Users\blah\Desktop\a\' is not recognized as开发者_Go百科 an internal or external command, operable program or batch file.


You can double quote your string literal. Edit: Also just noticed that your backslashes were not escaped so updated below :P

system("\"C:\\Users\\blah\\Desktop\\a\\ b.txt\"");

Also let it be known for the record that you really shouldn't use system. Try fork, spawn, or perhaps even the unofficial boost.process class which has functionality similar to .NET process class depending on your needs. Also think about why you need launch a process from a process ... perhaps you could make a library?


On Unix, you could use fork() + exec().

On Windows, check out spawn.

These execute the program directly, avoiding the command shell interpreter, thus avoiding any special treatment of special characters like spaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消