Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > system function and Whitespaces - Windows

Reply
Thread Tools

system function and Whitespaces - Windows

 
 
jean.daniel.michaud@gmail.com
Guest
Posts: n/a
 
      06-26-2007
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

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 1:28 pm, (E-Mail Removed) wrote:
> 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.")



 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
>
> Here is a little program:


Since C++ is not C, C++ code is, of course, not topical in
<news:comp.lang.c>.
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.

> 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
>

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      06-26-2007
On Tue, 26 Jun 2007 10:28:44 -0700, in comp.lang.c ,
(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
chopstickz9999@gmail.com
Guest
Posts: n/a
 
      06-27-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Remove whitespaces and line breaks in a XML file Stefan Behnel Python 1 02-09-2011 07:23 AM
REXML - controlling whitespaces and inserting CR Charles Wel Ruby 6 08-12-2010 08:36 PM
calling system() with whitespaces tom_kuehnert@gmx.de C++ 8 06-26-2008 09:17 AM
Re: XML: How to remove whitespaces from element's text? Joona I Palaste Java 1 01-07-2004 06:44 PM
XML: How to remove whitespaces from element's text? Berco Java 0 01-07-2004 06:31 PM



Advertisments