473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

calling system() with whitespaces

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
Jun 27 '08 #1
12 3442
On Jun 24, 12:00*pm, tom_kuehn...@gmx.de wrote:
I tried putting all paths in double quotes:
system("\"C:\A B\C.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.
Jun 27 '08 #2
(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!
Jun 27 '08 #3
to**********@gmx.de kirjutas:
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.
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
Jun 27 '08 #4
On 2008-06-24 17:54:45, Paavo Helde wrote:
to**********@gmx.de kirjutas:
>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.

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.
Tom, does the command line you pass to system() run within cmd.exe? (Of
course the escape characters properly stripped.) That would be the first
test.
I understand you are working under Windows. In this case you should use
ShellExecute() or ShellExecuteEx(), these are Windows facilities for
calling the "shell".
Shouldn't system() do the same (with less control, of course)?
On Linux OTOH you could use proper escaping with backslashes.
Well... that this is more "proper" than enclosing the path in double quotes
is probably not a hard fact :)

Gerhard
Jun 27 '08 #5
Gerhard Fiedler <ge*****@gmail.comkirjutas:
On 2008-06-24 17:54:45, Paavo Helde wrote:
>to**********@gmx.de kirjutas:
>>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.

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.

Tom, does the command line you pass to system() run within cmd.exe?
(Of course the escape characters properly stripped.) That would be the
first test.
>I understand you are working under Windows. In this case you should
use ShellExecute() or ShellExecuteEx(), these are Windows facilities
for calling the "shell".

Shouldn't system() do the same (with less control, of course)?
If it has less control it can't be the same ;-)

One thing: system() always creates a black DOS box on the screen which
many people do not like, even if it is the native Windows application
which is started (because CMD.EXE is a console application).

Also, the timing behavior of system() on Windows has been historically
different. I believe in earlier times system() returned immediately
without even waiting for the termination of the launched command.
Nowadays it seems to wait; one can use START inside the command to launch
an application asynchronously if that's what's wanted.

The advantage of system() in Unix world (compared with spawn/exec
families) is that one can use the exact same command line as in the
shell. In Windows the default shell is so poor, multiply patched over the
years and having hard to control deviations in behavior ("Command
Extensions"), so that using it does not buy much IMHO.

>On Linux OTOH you could use proper escaping with backslashes.

Well... that this is more "proper" than enclosing the path in double
quotes is probably not a hard fact :)
It is just easier to get right if the arguments may contain double quotes
or any other strange characters by themselves. And unknown directory
names most certainly can contain double quotes, for example.

All the best,
Paavo
Jun 27 '08 #6
@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.
Its already the C.exe which cannot be found...
And I wanted to use system() to stay on a level that could be used in
both - Windows and Linux (having to correct the slashes only).

@Gerhard
Tom, does the command line you pass to system() run within cmd.exe? (Of
course the escape characters properly stripped.) That would be the first
test.
I get a bit confused when trying to pass the argument directly to the
cmd.exe, since it opens just a new shell and seems to ignore the
parameters. I might be wrong with the assumption that the whole thing
is passed to cmd.exe as argument. This was just what I figured from
debugging into the system() command...

@ me

I gave up on the system() command by now and using spawn () now. This
allows me also an asynchronous operation which I need for another .exe
call a few lines later anyway... The command window produced by those
procedures isnt a problem since my 3D app has one of them itself and
output of the procedures called then appear in that one (at least
under windows).

Thanks for the advice from you all,
I will do some tests on the orignial problem when I've got some time
again - it still seems strange to me...

Thx+cu
Tom
Jun 27 '08 #7
On Jun 25, 9:16 am, tom_kuehn...@gmx.de wrote:
@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.
Its already the C.exe which cannot be found...
And I wanted to use system() to stay on a level that could be
used in both - Windows and Linux (having to correct the
slashes only).
There's no problem using slashes ('/') as directory separators
under both Windows and Linux; I'd generally recommend it, in
fact. The problems will be that the pathname itself won't be
the same; for historical reasons, Windows and Unix organize
their directories differently.

Beyond that, how system works is pretty much implementation
dependent. Posix requires that it execute:
execl(<shell path>, "sh", "-c", command, (char *)0);
(where <shell pathis the path to the Posix shell), but I
wouldn't be surprised if some implementations took the $SHELL
environment variable into account, or under Linux, simply
invoked bash as bash (and not as sh). Under Windows, it's even
more varied.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
On 2008-06-25 02:26:36, Paavo Helde wrote:
>>I understand you are working under Windows. In this case you should
use ShellExecute() or ShellExecuteEx(), these are Windows facilities
for calling the "shell".

Shouldn't system() do the same (with less control, of course)?

If it has less control it can't be the same ;-)
Yes, I expressed myself poorly -- but I guess you knew what I meant :)
One thing: system() always creates a black DOS box on the screen which
many people do not like, even if it is the native Windows application
which is started (because CMD.EXE is a console application).
I think that's what system() is for -- calling the shell. So this shouldn't
be surprising. (You're of course correct in that there is little point to
try to make it portable by calling system() because the details of the
shell will be varying widely -- on Linux not less than on Windows,
probably.)

>>On Linux OTOH you could use proper escaping with backslashes.

Well... that this is more "proper" than enclosing the path in double
quotes is probably not a hard fact :)

It is just easier to get right if the arguments may contain double quotes
That (arguments that may contain double quotes) would be a poor choice on
the side of the application developer. I'm not sure, but I think there's no
reliable way (if there is a way at all) to pass double quotes in arguments
through batch files. (But yes, that's a limitation with this scheme.)
or any other strange characters by themselves.
Is there a problem with "strange" characters (besides double quotes)?
And unknown directory names most certainly can contain double quotes, for
example.
Are you sure? This here
<http://msdn.microsoft.com/en-us/library/aa365247.aspxseems to say that
double quotes (and a few other characters) are not allowed on Windows.

Gerhard
Jun 27 '08 #9
Gerhard Fiedler <ge*****@gmail.comkirjutas:
On 2008-06-25 02:26:36, Paavo Helde wrote:
[...]
>
>And unknown directory names most certainly can contain double quotes,
for example.

Are you sure? This here
<http://msdn.microsoft.com/en-us/library/aa365247.aspxseems to say
that double quotes (and a few other characters) are not allowed on
Windows.
I was talking about Linux branch, sorry I was not clear enough. In Windows
quoting by backslashes does not work anyway. I think they invented some
other character for such quoting, but I don't remember right now what it
is.

Regards
Paavo
Jun 27 '08 #10
to**********@gmx.de kirjutas:
>
I get a bit confused when trying to pass the argument directly to the
cmd.exe, since it opens just a new shell and seems to ignore the
parameters.
It seems you are missing the /C parameter.

Regards
Paavo
Jun 27 '08 #11
On 2008-06-25 13:42:45, Paavo Helde wrote:
Gerhard Fiedler <ge*****@gmail.comkirjutas:
>On 2008-06-25 02:26:36, Paavo Helde wrote:
[...]
>>
>>And unknown directory names most certainly can contain double quotes,
for example.

Are you sure? This here
<http://msdn.microsoft.com/en-us/library/aa365247.aspxseems to say
that double quotes (and a few other characters) are not allowed on
Windows.

I was talking about Linux branch, sorry I was not clear enough.
Under Linux, you'd generally use backslash quoting, so enclosing paths in
double quotes doesn't seem to apply.
In Windows quoting by backslashes does not work anyway. I think they
invented some other character for such quoting, but I don't remember
right now what it is.
I think the "proper" way to specify paths with spaces (and other characters
that may be interpreted otherwise) is to enclose them in double quotes.
Which works because double quotes are not allowed inside paths.

Gerhard
Jun 27 '08 #12
>It seems you are missing the /C parameter.
Oops :)

Thx
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: talisman | last post by:
hi, one of my friends was saying it's not a good idea to do this. certainly on my Solaris box man system makes a similar statement. could anyone elaborate in practicality how risky this is?...
4
by: 1qa2ws | last post by:
Hi, I need to get the windows current logged username and domain. Is it any possibility? 1qa2ws
5
by: Bennett Haselton | last post by:
I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from...
0
by: Diffident | last post by:
Hello Guys, How can I call a SYSTEM stored procedure in ORACLE from my DAL component? I am trying to call in the same way as I would call a normal stored procedure but it is throwing me an error...
0
by: Mian | last post by:
Hi i m developing an application for security systems where i m going to use a webcam to take sanps and store them at a specified location, obviously with a meaningful name. I would like to...
3
by: Me | last post by:
I am trying to figure out any issues with calling System.Reflection.MethodInfo.Invoke() when dealing with multiple threads. For instance.. Say I have a class that allows you to pass in a...
2
by: stevie.greenslade | last post by:
'Lo all. I have a simple question I'd like help on. :] How, if at all, would one go about suppressing the console window that flashes up when you make at system() call in your C++ program? I'm...
4
by: asearle | last post by:
Hi everyone, I have a perl script that opens an Access mdb thus: system("C:\\Documents\ and\ Settings\\asearle\\My\ Documents\\iwh_dev\\MyAccessDB.mdb"); This works absolutely fine and,...
1
by: Matthew Lock | last post by:
Hello, I get some strange behaviour after calling a method on a third party library when I call: System.Diagnostics.Process.GetCurrentProcess().StartTime If I run the following code it works as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.