简体   繁体   English

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

[英]Exchanging info between running C++ code and python script

I have a python script that is called inside a C++ program. 我有一个在C ++程序中调用的python脚本。 The python script creates a directory based on the current time, puts files in there, and then execution returns to C++. python脚本根据当前时间创建一个目录,将文件放入其中,然后执行返回到C ++。 I want to save a parameters file into the directory created in the python program. 我想将参数文件保存到python程序中创建的目录中。

I figure my options are: 我认为我的选择是:

  1. Pass in the text to save in the parameters file to the Python program and have it create and save the file 传递文本以将参数文件保存到Python程序,并让其创建并保存文件
  2. Return the location of the directory to C++ from python so it knows where to save the file 将目录的位置从python返回到C ++,以便知道将文件保存在哪里
  3. Use C++ to locate the most recently created directory after execution of the python script and put file there 执行python脚本后,使用C ++定位最新创建的目录,并将文件放在此处

I'm not sure how to do any of this. 我不知道该怎么做。 My python script is not embedded. 我的python脚本未嵌入。 I use 我用

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

to call a python script. 调用python脚本。

Any ideas how to do this? 任何想法如何做到这一点?

I'd go with option B -- return the location of the directory to c++ from python so it knows where to save the file. 我会选择选项B-从python返回目录的位置到c ++,以便它知道将文件保存在哪里。

If you plan on using system(), something like this: 如果计划使用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 */
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM