1

I'm working on a program that wraps a C++ program that mutates a nucleotide sequence with Python. I'm much more familiar with python then I am with C++ and parsing data files is easier for me using Python.

How do I take a string that I've parsed in Python, and use that as an input to the C++ program? The C++ program by itself already takes strings, input by users, as an input.

Anthon
  • 69,918
  • 32
  • 186
  • 246
Andy N.
  • 31
  • 1
  • 3
  • 1
    _Wraps_ how actually? – πάντα ῥεῖ May 24 '15 at 21:23
  • Can you explain what do you want exactly? Parse in C++ in Python-style, or parse Python-parsed strings to C++? – Lapshin Dmitry May 24 '15 at 21:25
  • So the C++ file, takes a string and changes the string according to some rules that have been set. I'm using python to extract strings from data files online, that would then be used as an input in the C++ file, but I have no idea how to do that. – Andy N. May 24 '15 at 21:32
  • Let's say you marshal your data into JSON, or CSV, or any other exchange format. Then, you'll have to parse it with C++. Why don't you simply parse your original data with C++? – Pierre Prinetti May 24 '15 at 21:45

3 Answers3

1

You can launch your python script as a separate process and get its complete output. In QT you can do this this way for example:

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.

If you are not QT familiar, use platform specific process manipulations techniques or boost. Check this out:

How to execute a command and get output of command within C++?

How to create a process in C++ on Windows?

Execute a process and return its standard output in VC++

Community
  • 1
  • 1
str14821
  • 243
  • 1
  • 10
0

If you mean calling a program from Python and doing something with its output, then you want the subprocess module.

If you want to expose your C++ function directly to Python, then I'd suggest checking out Boost.Python.

Barry
  • 286,269
  • 29
  • 621
  • 977
0

Do you want to take the output of a python program and use it as input to a C++ program?

You could just use the shell for that:

python ./program.py | ./c_program  

Do you want to execute a python program from within C++ and get the output back as a string?
There are probably better ways to do it, but here is a quick solution:

//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");
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271