4
A
回答
10
嘗試
system("./helloworld.sh");
3
嘗試system()。
1
在C中還有execxxx
functions從unistd.h
。與簡單的system
相比,它們有很大的優勢,因爲您可以指定要在進程參數管理的其他控制級別中運行的進程的環境變量。
0
,如果你也想拿到劇本的輸出做
char fbuf[256];
char ret[2555];
FILE *fh;
if ((fh = popen("./helloworld.sh", "r")) == NULL) {
return 0;
}else{
while (fgets(fbuf, sizeof(fbuf), fh)) {
strcat(ret, fbuf);
}
}
pclose(fh);
5
如果你只是想如果你需要獲得標準輸入來運行它(沒有別的)
system("./helloworld.sh");
/stdout那麼你需要使用popen()
FILE* f = popen("./helloworld.sh","r");
1
至少有兩種可能的方法。 (我想你在問使用shell腳本的類Unix系統)。
第一個是非常簡單的,但是阻塞(它返回的命令已經完成後):
/* Example in pure C++ */
#include <cstdlib>
int ret = std::system("/home/<user>/helloworld.sh");
/* Example in C/C++ */
#include <stdlib.h>
int ret = system("/home/<user>/helloworld.sh");
的第二種方式是不容易,但可能是無阻塞(腳本可以作爲運行並行處理):
/* Example in C/C++ */
#include <unistd.h>
pid_t fork(void);
int execv(const char *path, char *const argv[]);
/* You have to fork process first. Search for it, if you don't know how to do it.
* In child process you have to execute shell (eg. /bin/sh) with one of these
* exec* functions and you have to pass path-to-your-script as the argument.
* If you want to get script output (stdout) on-the-fly, you can do that with
* pipes. Just create the reading pipe in parent process before forking
* the process and redirect stdout to the writing pipe in the child process.
* Then you can just use read() function to read the output whenever you want.
*/
+0
由於OP要求採用C++方式,所以不應該提及像fork/execv ...和btw這樣的東西,當您已經使用fork時,沒有理由使用execv而不是execv,還是存在? – smerlin 2010-07-07 22:51:27
相關問題
- 1. 如何在C中執行shell命令?
- 2. 如何在iPhone中執行命令行?
- 3. 在c#中使用DbCommand執行命令#
- 4. 如何從C++程序執行命令行命令
- 5. 如何從命令行在iTerm窗口中執行命令?
- 6. 在C++中調用命令行程序
- 7. 如何在c#中執行超過1行的sql命令
- 8. 如何:在C#中執行命令行,獲取STD OUT結果
- 9. 使用C執行命令行#
- 10. 在C#上執行PowerShell命令行
- 11. 如何執行的命令的命令使用C#
- 12. 如何從c#執行cmd命令#
- 13. 在c程序中執行SET命令
- 14. 如何在java中執行cmd命令?
- 15. 如何在MSI中執行命令?
- 16. 如何在Kotlin中執行bash命令
- 17. 如何在Java中執行Windows命令?
- 18. C#執行cmd命令
- 19. 執行shell命令(c)
- 20. C++執行CMD命令
- 21. c#執行命令行如命令提示符?
- 22. PHP如何執行命令
- 23. 如何在gdb斷點的命令中執行和執行更多命令
- 24. 如何在執行命令之前回顯命令行?
- 25. 如何獲取在命令行執行的命令?
- 26. 如何調試C#命令行程序
- 27. 使用bash執行命令調用R
- 28. 在Node.js中執行命令
- 29. 在Linux中執行命令
- 30. 在javascript中執行命令
這是在「」頭或「」如果你喜歡這更好的。 –
mooware
2010-07-07 21:34:05
當你包含''時,它應該是'std :: system' - 參見http://stackoverflow.com/questions/2900785/ –
MSalters
2010-07-08 13:46:14