system function and Whitespaces - Windows

Discussion in 'C Programming' started by jean.daniel.michaud@gmail.com, Jun 26, 2007.

  1. Guest

    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
    , Jun 26, 2007
    #1
    1. Advertising

  2. Lew Pitcher Guest

    On Jun 26, 1:28 pm, 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.")
    Lew Pitcher, Jun 26, 2007
    #2
    1. Advertising

  3. 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
    >
    Martin Ambuhl, Jun 26, 2007
    #3
  4. On Tue, 26 Jun 2007 10:28:44 -0700, in comp.lang.c ,
    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
    Mark McIntyre, Jun 26, 2007
    #4
  5. Guest

    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.
    , Jun 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste
    Replies:
    1
    Views:
    476
    Berco
    Jan 7, 2004
  2. Replies:
    8
    Views:
    353
  3. Stefan Behnel
    Replies:
    1
    Views:
    980
    David Vicente
    Feb 9, 2011
  4. Charles Wel
    Replies:
    6
    Views:
    89
    Charles Wel
    Aug 12, 2010
  5. Replies:
    2
    Views:
    86
    Richard Balbat
    Nov 12, 2013
Loading...

Share This Page