首页 > 解决方案 > 将 python 脚本的输出读入 C 程序而不创建临时文件以在程序之间传递值

问题描述

我正在使用system("calling python script")<- 这是在 c 程序中调用的。在脚本中,我只是检查两个数组是否匹配然后返回这个

下面的行是从 C 程序触发的

然后我读取了系统返回值,它只返回 0。我要做的就是将一个值从 python 传递给 C 程序,而不在它们之间使用临时文件。

C程序:

#include <stdio.h>
#include <stdlib.h>

char buffer[300];
snprintf(buffer, sizeof(buffer), "xls2csv %s > /home/......../xyz.py", filename);

int systemReturn = system(buffer);

if(systemReturn == -1)
{
    // The system method failed
    fprintf(stderr, "Error");
}
if(systemReturn = 2 )
{
    //Then do this
}

蟒蛇程序:

if(Data_in_end != merged):
    print("They did not match")
    sys.exit(1)
else:
    print("They did match")
    sys.exit(2)

标签: pythoncpython-3.xgtk

解决方案


如果你只是想在程序之间传递一点数据,你可以使用参数。例如,linuxshutdown命令默认会在一分钟内关闭您的计算机。但是,如果您将参数“now” ( shutdown now) 传递给它,它将立即关闭。

在您的 c 程序中,您将需要一个接受参数的特殊格式的 main 函数。网上有数百个例子。对于大量数据,不建议使用此方法。


推荐阅读