Hey, it’s me again. I’m currently looking for a way to capture an output from a write\1, so I can manipulate the string as I want.
Here is the example:
PlCall("assert", PlTermv(PlCompound("myprint :- write('soy un poco loco')")));
PlCall("myprint");
With this code I get the standard output in my cmd with the string:
soy un poco loco
But I want, for example, put it in a string and manipulate it without using standard output redirection if possible
The generic C++ interface to streams is PlStream, but it’s a fairly thin wrapper on the C API.
If you want a stream for writing to memory, see Sopenmem() – there’s an example of its use in packages/odbc/ofbc.c. You can convert the IOSTREAM* to a PlStream. See also PlAcquireStream.
(I’ve wanted this a few times, so maybe it’s worthwhile making a nice C++ API for writing to a string.)
I’m not sure. There may be exceptions, but in general you do not want to exchange text when combining Prolog with some other language.
Playing around with the standard streams is rather tricky, so possibly using with_output_to/2 is a good idea anyway … You already have to call Prolog anyway. The main reason to do it from C++ might be if there is a huge amount of output to be captured, i.e., more than what fits on the Prolog stack.
And it worked!
Don’t pay attention to openPrologFile, it’s just a quick procedure I added to quickly prompt PlCall("consult",...) with exception handling.