system function and Whitespaces - Windows

  • Thread starter jean.daniel.michaud
  • Start date
J

jean.daniel.michaud

Hi all,

Here is a little program:

// Snippet on
#include <stdlib.h>
#include <iostream>

int main(int argc, char **argv)
{
char *command = "\"C:\\Program Files\\PSPad\\PSPad.exe\" \"c:\\file
whitespace.txt\"";
std::cout << command << std::endl;
system(command);
return 0;
}
// snippet off

It just says that 'C:\Program' is not a valid command. But when I
type:

"C:\Program Files\PSPad\PSPad.exe" "c:\file whitespace.txt"

in a dos command prompt, it works. For some reasons I don't want to
use the 8 characters version (C:\Progra~1) which actually works.

Any idea?

JD
 
L

Lew Pitcher

Hi all,

Here is a little program:

// Snippet on
#include <stdlib.h>
#include <iostream>

int main(int argc, char **argv)
{
char *command = "\"C:\\Program Files\\PSPad\\PSPad.exe\" \"c:\\file
whitespace.txt\"";
std::cout << command << std::endl;

First off, this source code is not written in C. You probably should
ask your question in comp.lang.c++, which is just down the hall, on
the right (just after the koolaid dispenser).
system(command);
return 0;}

// snippet off

It just says that 'C:\Program' is not a valid command. But when I
type:
[snip]

Well, you'll have to go back to the documentation your compiler
supplied for the system() function to see what is wrong.
/If/ this were a C program, then we would tell you the same thing, as
the format and contents of the single argument passed to the C
system() function is implementation-dependent ("If string is not a
null pointer, the system
function passes the string pointed to by string to that command
processor to be executed /in a manner which the implementation shall
document/ (emphasis mine); this might then cause the program calling
system to behave in a non-conforming manner or to terminate.")
 
M

Martin Ambuhl

Hi all,

Here is a little program:

Since C++ is not C, C++ code is, of course, not topical in
<In C, the behavior of system() and the interpretation of its argument
are almost entirely up to the implementation. Very little (apart from
the behavior with a NULL argument) can be said without recourse to
implementation-specific behavior, which is properly addressed in
newsgroups, mailing lists, etc. for your implementation, but only after
you have checked your documentation.

Having issued the disclaimers, let's move on.
Suppose we replace your definitely-not-C and not-C++-as-normally-written
code
#include <stdlib.h>
#include <iostream>

int main(int argc, char **argv)
{
char *command = "\"C:\\Program Files\\PSPad\\PSPad.exe\" \"c:\\file
whitespace.txt\"";
std::cout << command << std::endl;
system(command);
return 0;
}

with this C code
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char *command =
"\"C:\\Program Files\\PSPad\\PSPad.exe\" "
"\"c:\\file whitespace.txt\"";
char *command2 =
"C:/Program Files/PSPad/PSPad.exe "
"c:/file whitespace.txt";

puts(command); /* mha: replaced multiply erroneous
absurdity */
printf("A command processor is %s available.\n",
(system(0)) ? "" : "not ");
printf("system(command) returned %d\n", system(command));

puts(command2);
printf("system(command2) returned %d\n", system(command));
return 0;
}

I have no idea what your implementation will do with this, but it may be
worth your exploring.
It just says that 'C:\Program' is not a valid command.

Obviously system in your implementation and the command processor that
it invokes think that the space after "Program" is a significant
separator. Check your implementation's documentation for how to
overcome this problem. You may need to replace the space with something
else; you may need to set some implementation-specific variables to
modify what shell is used and how it is used. None of this has anything
to do with C, but with your implementation.
 
M

Mark McIntyre

On Tue, 26 Jun 2007 10:28:44 -0700, in comp.lang.c ,
char *command = "\"C:\\Program Files\\PSPad\\PSPad.exe\" \"c:\\file
whitespace.txt\"";
system(command);

It just says that 'C:\Program' is not a valid command.

This is nothing to do with C. Its all to do with how the Windows
runtime environment parses and executes commandlines wih embedded
spaces. You will need to ask in an MSWindows group for more advice.
<OT>
my recollection is that you need to explicitly run a command shell
</ot>

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

chopstickz9999

First, this poor guy just asked a simple question why you guys so
kindly
gave him so muchcrapy info. Only this one went straight.
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Besides, he already said he is not good with C.

Just kidding. Don't know there are still good C people around.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top