ming-4

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

分类:

技术点:

相关文章:

  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2021-11-10
  • 2022-12-23
  • 2021-10-11
猜你喜欢
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-12-09
  • 2022-02-10
相关资源
相似解决方案