I have a Rails app that runs a C++ executable from the command line. I'm able to print the C++ cout
to the command line, but I'd like to assign it back to a variable, output =
in a Rails controller.
Is this possible?
Below is a simplified example of my Rails controller action and C++ .cpp
to help explain the question.
Rails controller:
def get_variable
system("cd ~/workspace/OutputTest/src && ./output.bin")
end
Note I've already compiled and created a C++ executable file named output.bin
.
C++ file:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!!!";
return 0;
}
I'm familiar with Rails (not necessarily running files from the command line through Rails), but a complete newb to C++.
Any guidance would be very appreciated. If there is another approach I should be taking completely, that would also be very helpful to know.
I would prefer not to do this inline, as I'd like to do a lot more in C++ once I can solve this initial step.
UPDATE
The best solution I've come up with so far is writing a string to a .txt
in C++, and then using File.read()
in Rails. But it doesn't feel scalable, or give flexibility over data structure.
So I'm still wondering if there is a more straightforward way, like somehow keeping the value in memory for Rails to access it. Not sure though.