none
Quoting arguments to system() RRS feed

  • Question

  • 	system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\""); // ok
    	system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\" \" \""); // 'c:\program' is not recognized as an internal or external command,
    operable program or batch file.
    
    

    How do I get 2 to work? 
    Wednesday, January 25, 2012 2:26 PM

Answers

  • Olaf vander Spek wrote:
    >
    >        system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\""); // ok
    >        system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\" \" \""); // 'c:\program' is not recognized as an internal or external command,
    >operable program or batch file.
    >
    >How do I get 2 to work? 
     
    Believe it or not:
     
        system("\"\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\" \" \"\"");
     
    That is, wrap the exe filename with quotes, wrap any parameters with spaces
    in them in quotes, then wrap the entire command string with quotes.
     
    You might consider using "execl" or one of the other "exec" family
    functions instead, or even CreateProcess.  You'll end up with less cruft.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.
    Thursday, January 26, 2012 4:58 AM

All replies

  • Olaf vander Spek wrote:
    >
    >        system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\""); // ok
    >        system("\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\" \" \""); // 'c:\program' is not recognized as an internal or external command,
    >operable program or batch file.
    >
    >How do I get 2 to work? 
     
    Believe it or not:
     
        system("\"\"c:\\program files (x86)\\cncmaps\\cncmaps.exe\" \" \"\"");
     
    That is, wrap the exe filename with quotes, wrap any parameters with spaces
    in them in quotes, then wrap the entire command string with quotes.
     
    You might consider using "execl" or one of the other "exec" family
    functions instead, or even CreateProcess.  You'll end up with less cruft.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.
    Thursday, January 26, 2012 4:58 AM
  • You might consider using "execl" or one of the other "exec" family functions instead,

    Are you sure?

    If successful, these functions do not return to the calling process.


    Thursday, January 26, 2012 11:40 AM
  • As Tim suggests,  CreateProcess() and other functions would be less complex for this case, although I'd consider _spawn() and friends before _exec().

    http://msdn.microsoft.com/en-us/library/20y988d2(v=vs.80).aspx

     

    Thursday, January 26, 2012 6:49 PM
  • How is CreateProcess() simpler than system()? With the extra quotes system() appears to work fine.
    Friday, January 27, 2012 9:49 AM
  • :-D   You're absolutely right! I need to give some context to explain what I should have said.
    First of all, as everyone who has tried knows, handling paths with spaces, backslashes and quotation marks in Windows is not intuitive. Issues with them happen frequently when creating BAT files (which are interpreted by cmd.exe).
    system() actually passes its string to the system's command line processor, cmd.exe; that is why system("dir > somewhere.txt") works. So system() it is not very different from calling cmd.exe with the /c switch and you are responsible for providing a command line that cmd can interpret.
    When you use CreateProcess(), you skip cmd.exe (which is valuable in my experience). Plus, its lpApplicationName and lpCommandLine parameters give some control about command line interpretation. However, you are still responsible for formatting things for proper interpretation.
    Finally, if you use the _spawnl(), _execl() or one of their friends you're specifying exactly what needs to be done and you're delegating responsibility for encoding the arguments properly right where it belongs: at Microsoft.
    But, by all means, if system() solves the problem you have, just use it. As everything, it all depends on your requirements.
    Sunday, January 29, 2012 11:51 PM