Windows C 系统调用,命令中有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2642551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:17:01  来源:igfitidea点击:

windows C system call with spaces in command

cwindowscommand-lineescaping

提问by myforwik

I cannot make system calls with spaces in the names and parameters. For example:

我无法使用名称和参数中的空格进行系统调用。例如:

system("c:\program files\something\example.exe c:\my files\example.txt");

I have tried escaping in every way I know how, and NOTHING works. I have tried:

我尝试过以我知道的各种方式逃避,但没有任何效果。我试过了:

system("\"c:\program files\something\example.exe\" \"c:\my files\example.txt\"");

and

system("c:\program^ files\something\example.exe c:\my^ files\example.txt");

Neither work. I still get 'c:\program' is not a recongnised internal or external command

都不工作。我仍然得到 'c:\program' is not a recongnised internal or external command

This is really driving me mad... I need to call and pass parameters that have spaces in them. I cannot use the short notation for reasons I won't go into.

这真的让我发疯......我需要调用并传递其中包含空格的参数。由于我不会讨论的原因,我不能使用短符号。

I have tried with ' quotes instead of " quotes, still doesn't work. I have tried putting quotes around the whole thing and quotes around the spaces and that doesn't work.

我试过用 ' 引号代替 " 引号,仍然不起作用。我试过在整个事物周围加上引号,在空格周围加上引号,但那不起作用。

Does anyone know how to do it properly?

有谁知道如何正确地做到这一点?

回答by EMP

Edit:OK, figured it out by stepping into the system()call: you need an extra set of quotes around the whole thing due the fact that it internally calls cmd /cyourinput

编辑:好的,通过进入system()调用来解决这个问题:由于它在内部调用cmd /c yourinput,因此整个事情需要一组额外的引号

So this works for me:

所以这对我有用:

system("\"\"c:\program files\internet explorer\iexplore.exe\" \"www.stackoverflow.com\"\"");

A bit of a mess, isn't it?

有点乱,不是吗?