C語言中調用運行python程序


C語言中調用運行python程序:

Python代碼如下:

創建test.py。

#!/usr/bin/python3
#test.py
import sys
x = int(sys.argv[1])
print x*x

注意:(#!/usr/bin/python3這一行代碼要根據自己的硬件情況而定)

C語言代碼如下:

創建test.c。

//test.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
        FILE *f;
        char s[10240];
        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;
}

測試如下:

$ gcc -o test test.c
$ ./test
9801


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM