开发者

How to overcome spaces in the path while passing to system()?

开发者 https://www.devze.com 2022-12-12 01:18 出处:网络
In the following statement : system(\"%TESTCASES_PATH%SIP\\\\test.bat\"); the %TESTCASES_PATH% gets resolved to \"C:\\Program Files...\" .

In the following statement :

system("%TESTCASES_PATH%SIP\\test.bat");

the %TESTCASES_PATH% gets resolved to "C:\Program Files..." .

As such the result of calling the system is :

"'C:\Program' is not recognized as an internal or external command.."

'C:\Program' is thought as a executable..

How to overcome the above issue?

EDIT: Trying out what is proposed in the answers, I get to see the same behavior. The following is the actual code:

#include <stdio.h>
#include<conio.h>

int ma开发者_运维百科in()
{
    system("\"%TESTCASES_PATH%SIP\\Provisioning\\CrHomeDnOfficeCodeDestCodeBySoap\\CreateHomeDnOfficeCode.bat\"");
    //system("\"%TESTCASES_PATH%SIP\\tests.bat\"");
    getch();

    return 0;
}


Use double quotes to pass the entire path as the executable / batch file:

system("\"%TESTCASES_PATH%SIP\\test.bat\"");

Otherwise, what's after a space becomes the first command-line parameter.

EDIT: Perhaps on your setup, %TESTCASES_PATH% is not expanded by the system() function. On most systems, you can retrieve the value of an environment variable with getenv():

char cmd[FILENAME_MAX];
snprintf(cmd, FILENAME_MAX, "\"%s\\test.bat\"", 
    getenv("TESTCASES_PATH"));
system(cmd);


What about:

system("\"%TESTCASES_PATH%SIP\\test.bat\"");

The additional double quotes in the string allow to pass file names with white space to the system call.


With one caveat to both solutions : test them with a string that contains NO space too.

It might fail on some windows shells.

0

精彩评论

暂无评论...
验证码 换一张
取 消