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?

1 Answer 1

-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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.