參考http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/popen.html
FILE *popen(const char *command, const char *type);
執行某指令並把stdout的值放入檔案並回傳
若要取得stderr可以在指令後加上 "2>&1"
用完後要使用pclose()關閉檔案
example:
/*
* Purpose: Program to demonstrate the popen function.
*
* to do: Check that the 'popen' was successfull.
*
* Author: M J Leslie.
* Date: 08-Jan-94
*/
#include
main()
{
FILE *fp;
char line[130]; /* line of data from unix command*/
fp = popen("ls -l", "r"); /* Issue the command. */
/* Read a line */
while ( fgets( line, sizeof line, fp))
{
printf("%s", line);
}
pclose(fp);
}
沒有留言:
張貼留言