1

I have a C++ program that, through the terminal, takes a text file as input and produces another text file. I'm executing this program from a Python script which first produces said text string, stores it to a file, runs the C++ program as a subprocess with the created file as input and parses the output text file back into a Python object.

Is it possible to do this without using a subprocess call? In other words: is it possible to avoid the reading and writing and just run the C++ program inside the Python environment with the text-string as input and then capture the output, again inside the Python environment?

For code I refer to the function community_detection_multiplex in this IPython notebook.

6
  • Do you have a C++ parser/interpreter written in Python? If yes, great. Otherwise, no. Commented Sep 5, 2016 at 16:34
  • spawning a subprocess isn't really a big deal is it? You could send the input/collect output via stdin/stdout pipe redirects if you want to avoid the security risk of the file. Commented Sep 5, 2016 at 16:35
  • @RichardHodges how would I pass the string to the C++ program from Python if it were never stored as a file? Commented Sep 5, 2016 at 16:45
  • @UlfAslak with a pipe. The subordinate program could just read data from stdin and write to stdout. These would map to the python program's pipe (which it would create). Commented Sep 5, 2016 at 16:48
  • @UlfAslak lots of info here: docs.python.org/3/library/… Commented Sep 5, 2016 at 16:50

1 Answer 1

3

You can use ctypes.
It requires the C++ function to be wrapped with extern "c" and compiled as C code.

Say your C++ function looks like that:

char* changeString(char* someString)
{
    // do something with your string
    return someString;
}

You can call it from python like that:

import ctypes as ct

yourString = "somestring"
yourDLL = ct.CDLL("path/to/dll") # assign the dll to a variable
cppFunc = yourDLL.changeString # assign the cpp func to a variable
cppFunc.restype = ct.c_char_p # set the return type to a string
returnedString = cppfunc(yourString.encode('ascii')).decode()

Now returnedString will have the processed string.

Sign up to request clarification or add additional context in comments.

5 Comments

Ideally I would want the file to not be stored, and it seems that your solution does this?
My solution calls a C++ function and pass a string to it. If you want to open a file in Python and pass it (while open) to C++, then this is not the right solution for you.
as I understand this solution has the string stored as a file
yourPathToFile is a string variable contains a path to a file (ideally, I would use the os.path module to create a path string). You can do with open(yourPathToFile) as f: and read the file in python. In the solution I posted, it is expected that the c++ function will open the file within itself.
I'm sorry I must not have explained it well enough. The python script generates the string which should be processed by the C++ program. That's why I want to avoid saving any files.

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.