新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么在c中運(yùn)行程序
C語(yǔ)言中運(yùn)行python程序

C語(yǔ)言使用popen/system或者直接以系統(tǒng)調(diào)用級(jí)fork+exec來(lái)運(yùn)行python程序也是一種混編的手段了。
舉例如下,Python代碼如下
#!/usr/bin/env python # test.py import sys x = int(sys.argv[1]) print x*x
C語(yǔ)言代碼如下
/* test.c */ #include#include int main() { FILE *f; char s[1024]; int ret; f = popen("./test.py 99", "r"); while((ret=fread(s,1,1024,f))>0) { fwrite(s,1,ret,stdout); } fclose(f); return 0; }
測(cè)試如下
$ gcc test.c $ ./a.out 9801
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:python怎么在c中運(yùn)行程序
文章鏈接:http://www.dlmjj.cn/article/dhjhihs.html


咨詢
建站咨詢
