gpt4 book ai didi

python - 在运行的 C++ 代码和 python 脚本之间交换信息

转载 作者:行者123 更新时间:2023-11-28 07:04:41 24 4
gpt4 key购买 nike

我有一个在 C++ 程序中调用的 python 脚本。 python脚本根据当前时间创建一个目录,将文件放在那里,然后执行返回到C++。我想将参数文件保存到在 python 程序中创建的目录中。

我认为我的选择是:

  1. 将要保存在参数文件中的文本传递给 Python 程序,并让它创建并保存文件
  2. 将目录的位置从 python 返回给 C++,以便它知道将文件保存在哪里
  3. 在执行python脚本后使用C++找到最近创建的目录并将文件放在那里

我不确定该怎么做。我的 python 脚本未嵌入。我用

    std::string test = "python analyzeData2.py";
system(test.c_str());

调用 python 脚本。

有什么办法吗?

最佳答案

我会选择选项 B——将目录的位置从 python 返回给 c++,这样它就知道将文件保存在哪里。

如果你打算使用 system(),像这样:

char* dirname[64];
FILE* fin;

system("python analyzeData2.py > created.log");
fin = fopen("created.log", "r");
fgets(dirname, sizeof(dirname), fin);
fclose(fin);
/* dirname has contents of created.log */
...

关于python - 在运行的 C++ 代码和 python 脚本之间交换信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21942619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com