使用空格调用system() [英] calling system() with whitespaces

查看:97
本文介绍了使用空格调用system()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在尝试使用system()执行程序。程序本身是

位于某个可能包含空格的路径中。


简单的解决方案就是:

system(& ; \" C:\ AB\C.exe \"");


但是现在这个程序还有一些需要传递的参数

(再次使用包含空格的路径)


我尝试将所有路径放在双引号中:

system(" \" C :\ AB\C.exe \" -a \" C:\ AB\D.txt \"");


但是它拒绝执行,因为C:\ A不是一条有效的道路。


有谁知道为什么会这样?我可以使用单引号

以某种方式来避免这个问题吗?

我的猜测是system()只是将整行传递给cmd.exe为

参数,将整行放在引号中。这导致我的

报价关闭那些太早。


我会感激任何帮助:)

Tom


PS:使用VS005

Hi!

I''m trying to execute a program using system(). The program itself is
located in some path which might contain whitespaces.

Simple solution would be this:
system("\"C:\A B\C.exe\"");

but now this program also has parameters which need to be passed
(again with paths containing whitepaces)

I tried putting all paths in double quotes:
system("\"C:\A B\C.exe\" -a \"C:\A B\D.txt\"");

But it refuses to execute since "C:\A" is not a valid path.

Has anyone got an idea why this happens? Can I use single quotes
somehow to avoid this problem?
My guess is that system() just passes the whole line to cmd.exe as
parameter, putting the whole line in quotes. This then results in my
quotes closing those "too early".

I''d appreciate any help :)
Tom

PS: using VS005

推荐答案

6月24日12:00 * pm,tom_kuehn ... @ gmx.de写道:
On Jun 24, 12:00*pm, tom_kuehn...@gmx.de wrote:

我尝试将所有路径放在双引号中:

system(" \" C:\ AB\C.exe \" -a \" C:\ AB\D.txt \"");
I tried putting all paths in double quotes:
system("\"C:\A B\C.exe\" -a \"C:\A B\D.txt\"");



也许你喜欢


system(" C:\\\ B \\\\ .exe \\ -a C:\\A\ B\\D.txt");


(与''\\'一起''''' '''和''\\'''与''\\''')


然后再说一次,多年来都没有尝试过类似的东西。

Maybe you something like

system("C:\\\ B\\C.exe\\ -a C:\\A\ B\\D.txt");

(escape '' '' with ''\ '' and ''\'' with ''\\'')

Then again, haven''t tried something like that in ages.


(Ups ...忘了这篇文章中的双反斜杠(但它们在那里

代码中))


Mh ......''\\'''无法正常工作:/ VS抱怨它是一个未知的

转义序列。


感谢您的回复!
(Ups...forgot the double backslashes in this post (but they are there
in the code))

Mh...the ''\ '' didn''t work :/ VS complaines about it as an unknown
escape sequence.

Thanks for the reply!


**** ****** @gmx.de kirjutas:
to**********@gmx.de kirjutas:

嗨!


我是尝试使用system()执行程序。程序本身是

位于某个可能包含空格的路径中。


简单的解决方案就是:

system(& ; \" C:\ AB\C.exe \"");


但是现在这个程序还有一些需要传递的参数

(再次使用包含空格的路径)


我尝试将所有路径放在双引号中:

system(" \" C :\ AB\C.exe \" -a \" C:\ AB\D.txt \"");


但是它拒绝执行,因为C:\ A不是一个有效的道路。
Hi!

I''m trying to execute a program using system(). The program itself is
located in some path which might contain whitespaces.

Simple solution would be this:
system("\"C:\A B\C.exe\"");

but now this program also has parameters which need to be passed
(again with paths containing whitepaces)

I tried putting all paths in double quotes:
system("\"C:\A B\C.exe\" -a \"C:\A B\D.txt\"");

But it refuses to execute since "C:\A" is not a valid path.



哪一个,.exe或.txt?如果它是.txt,则必须责怪C.exe

,因为他无法正确处理带有空格或引号的文件名。


我知道你在Windows下工作。在这种情况下,您应该使用

ShellExecute()或ShellExecuteEx(),这些是用于调用shell的

的Windows工具。在Linux OTOH上你可以使用

反斜杠进行适当的转义。


hth

Paavo

Which one, the .exe or .txt? If it''s .txt, then one has to blame C.exe
because he can''t cope with filenames with spaces or quotes properly.

I understand you are working under Windows. In this case you should use
ShellExecute() or ShellExecuteEx(), these are Windows facilities for
calling the "shell". On Linux OTOH you could use proper escaping with
backslashes.

hth
Paavo


这篇关于使用空格调用system()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆