从C ++开始,有没有办法进行系统调用以运行node process-file.js ./target_folder/
?
我的C ++项目的标准调用是:
node ../folderA/subfolder/process-file.js firstArgument ./target_folder/outputFile.txt
答案 0 :(得分:1)
const char *programname = "/usr/local/bin/node";
const char **nargv = new const char* [5];//extra room for program name and sentinel
nargv [0] = programname; // by convention is program name
nargv [1] = "/folder/convert-json.js"; // arg 1 to node
nargv [2] = "/folder/723edc747c39.json"; // arg 2
nargv [3] = "/tmp/data.json"; // arg 3
nargv [4] = NULL; // end of arguments sentinel is NULL
pid_t pid = fork();
if (pid == 0) /* child */ {
if (execv(programname, (char **)nargv) == -1) {
/* Handle error */
std::cout << "!! an error in calling node\n";
}
_exit(1); /* in case execv() fails */
}
我建立在你的身上(@ josh-olson)。这段代码可以帮助其他人。
答案 1 :(得分:0)
你可以使用fork()代替system(),如图所示:
char *filename = /* something */;
pid_t pid = fork();
if (pid == 0) /* child */ {
if (execve(filename, NULL, NULL) == -1) {
/* Handle error */
}
_exit(1); /* in case execve() fails */
}