How to run executable with spaces using std :: system on Windows

How to run an executable with spaces using std :: system on Windows without using C ++ 11?

I tried the seemingly obvious placement of quotation marks around the path with spaces, but in the console window that appears when the command starts, I get a message indicating that the full executable path is divided into spaces. For example, I tried the following:

#include <cstdlib>

int main()
{
    int no_spaces_forward_rc = std::system("c:/IronPython2.7/ipy -c \"print 'no_spaces_forward'\"");
    // no_spaces_forward_rc is 0 and "no_spaces_forward" is written to console window

    int spaces_forward_rc    = std::system("\"c:/Program Files (x86)/IronPython 2.7/ipy\" -c \"print 'spaces_forward'\"");
    // spaces_forward_rc is 1, and "'c:/Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int no_spaces_backward_rc = std::system("c:\\IronPython2.7\\ipy -c \"print 'no_spaces_backward'\"");
    // no_spaces_backward_rc is 0 and "no_spaces_backward" is written to console window

    int spaces_backward_rc    = std::system("\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\" -c \"print 'spaces_backward'\"");
    // spaces_backward_rc is 1, and "'c:\Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int no_spaces_double_backward_rc = std::system("c:\\\\IronPython2.7\\\\ipy -c \"print 'no_spaces_double_backward'\"");
    // no_spaces_double_backward_rc is 0, and no_spaces_double_backward is written to console window

    int spaces_double_backward_rc    = std::system("\"c:\\\\Program Files (x86)\\\\IronPython 2.7\\\\ipy\" -c \"print 'spaces_double_backward'\"");
    // spaces_double_backward_rc is 1, and "'c:\\Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int spaces_double_double_backward_rc    = std::system("\\\"c:\\\\Program Files (x86)\\\\IronPython 2.7\\\\ipy\\\" -c \"print 'spaces_double_double_backward'\"");
    // spaces_dobule_double_backward_rc is 1, and "'\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\"' is not recognized as an internal or external command, operable program or batch file." is written to console window

    return 0;
}

I checked that it works "c:\Program Files (x86)\IronPython 2.7\ipy" -c "print 'spaces_backward'"directly on the cmd command line, and I'm sure I have more than a typo. It drives me crazy - any help would be greatly appreciated!

(I am using Visual Studio 2012 and compiling using the Console subsystem if this helps.)

+4
2

cmd.exe . cmd.exe /?:

1.  If all of the following conditions are met, then quote characters
    on the command line are preserved:

    - no /S switch
    - exactly two quote characters
    - no special characters between the two quote characters,
      where special is one of: &<>()@^|
    - there are one or more whitespace characters between the
      two quote characters
    - the string between the two quote characters is the name
      of an executable file.

, std::system /S . , . , :

std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" h:\\documents\\documentation\\misc\\calendar 1999.pdf");

, , :

std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" \"h:\\documents\\documentation\\misc\\calendar 1999.pdf\"");

, , , :

std::system("\"\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" \"h:\\documents\\documentation\\misc\\calendar 1999.pdf\"\"");

std::system("\"\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\" -c \"print 'spaces_backward'\"\"");
+5

OK , raw string literal, (VS2013 Express):

std::string cmd = R"cmd("C:\Program Files (x86)\doxygen\bin\doxygen.exe")cmd";
system(cmd.c_str());

:

std::string cmd = "\"C:\\Program Files (x86)\\doxygen\\bin\\doxygen.exe\"";

, system() cmd, ", .


1) , , \ -.

0

All Articles