3

I have been looking around the site for a similar answer and the closest I managed to find is this one. The answer gives the following C program for achieving this:

//My code file
#include <stdio.h>
#include <Python.h>

void main()
{
    FILE* file;
    int argc;
    char * argv[3];

    argc = 3;
    argv[0] = "mypy.py";
    argv[1] = "-m";
    argv[2] = "/tmp/targets.list";

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

I would like to do something similar to this (using python 3, I don't know if that makes a difference) and generate an output using matplotlib. However I don't know how to adapt this to a C++ program, especially given the types required by the functions (such as wchar_t** for argv). Is there a way to do this without having to perform various typecasts to C types?

Luke Collins
  • 1,433
  • 3
  • 18
  • 36

1 Answers1

-2

I am not sure what Python.h gives you the ability to do, but if you just want to run a cmd command you could do this:

char* filename = "mypy.py";
char* arg = "-m";
char* argVal = "/tmp/targets.list";

char temp[512];

sprintf(temp, "python3 %s %s %s", filename, arg, argVal); 

int errCode = system((char *)temp);

if (errCode){
    std::cout << "Error code returned: " << errCode << std::endl;
    return 1;
}
remi
  • 937
  • 3
  • 18
  • 45