将Python的输入输入到c++程序中
Taking an input from Python into a C++ program
我正在编写一个程序,该程序包装了一个用Python改变核苷酸序列的c++程序。我对python比c++更熟悉,使用python解析数据文件对我来说更容易。
我如何在Python中解析一个字符串,并将其用作c++程序的输入?c++程序本身已经接受用户输入的字符串作为输入。
您可以将python脚本作为一个单独的进程启动,并获得其完整的输出。在QT中,您可以这样做,例如:
QString pythonAddress = "C:\Python32\python.exe";
QStringList params;
params << "C:\your_script.py" << "parameter2" << "parameter3" << "parameter4";
p.start(pythonAddress, params);
p.waitForFinished(INFINITE);
QString p_stdout = p.readAll().trimmed(); // Here is the process output.
如果您不熟悉QT,请使用平台特定的进程操作技术或boost。看看这个:
如何在c++中执行命令并获得命令的输出?
如何在Windows上用c++创建进程?
在vc++中执行一个进程并返回它的标准输出
如果您的意思是从Python调用程序并对其输出进行处理,那么您需要subprocess
模块。
如果你想将c++函数直接暴露给Python,那么我建议查看Boost.Python.
您想将python程序的输出用作c++程序的输入吗?
你可以直接使用shell:
python ./program.py | ./c_program
你想在c++中执行一个python程序并得到一个字符串的输出吗?
可能有更好的方法,但这里有一个快速的解决方案:
//runs in the shell and gives you back the results (stdout and stderr)
std::string execute(std::string const& cmd){
return exec(cmd.c_str());
}
std::string execute(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
if (result.size() > 0){
result.resize(result.size()-1);
}
return result;
}
std::string results_of_python_program = execute("python program.py");
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 无法在windows控制台中为C++程序提供必要的输入
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我的程序没有从文件中读取并输入数据
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 编写一个不断要求用户输入的程序
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- 如何将输入C 程序的平均值输入
- Sudoku输入程序跳过提示
- 无法将文件输入 C++ 程序
- 简单的c++输入程序
- 简单输入程序崩溃
- 用户用于输入程序设置的文件约定