0

I need to send the values of some variables from a Python script to a program "a.exe" written on C. To understand, the source of "a.exe" is here:

#include <stdio.h>
int main(){
double x1,x2;
scanf("%lf  %lf", &x1, &x2);
printf("%lf+%lf=%lf", x1,x2,x1+x2);
return 0;
}

The only thing I managed to find is to send the keyboard input, not variables:

import subprocess
p = subprocess.Popen("a.exe", stdout=subprocess.PIPE)
print ( p.stdout.read() )

That script works but I need to send to "a.exe" the values of variables xx1,xx2 instead. The script should look something like this:

import subprocess
xx1=1.;xx2=2.
p = subprocess.Popen("a.exe", stdout=subprocess.PIPE)
############

it seems that there must exist a very simple solution. What to put instead of the line with ############ (or what to do in fact) so as it worked?

Serge
  • 11
  • 3
  • 1
    Put the command and its args into a list, eg `subprocess.Popen(["a.exe", "1.0", "3.14159"], stdout=subprocess.PIPE)`. `a.exe` will receive that list as its `argv` array, so you need to declare it as `int main(int argc, char *argv[])` – PM 2Ring Dec 30 '16 at 09:36
  • Alternatively, you can pipe a string from Python into `a.exe` by giving `subprocess.Popen` a `stdin=subprocess.PIPE` arg. FWIW, it's usually simpler to use the convenience function `subprocess.call` (or `subprocess.run` on more recent versions of Python) rather than calling the `subprocess.Popen` constructor directly. – PM 2Ring Dec 30 '16 at 09:45
  • I would not tell this topic is an exact duplicate of the one indicated by @PM2Ring. Sure, there is many information in there that could help to solve my problem but I, not being a great expert in both C and Python, would have difficulty in extracting from there all the details in correct syntax that are really necessary in this specific case. Now I have a reply to my question, even in two variants. I will show them here, maybe it will be useful to somebody else. The Python part of the code below is formatted for Python 3. – Serge Jan 02 '17 at 16:51
  • The first variant, using stdin, PIPE and scanf in C part: `import subprocess` `p=subprocess.Popen('a.exe', stdin=subprocess.PIPE)` `xx1 = 1.; xx2 = 2.` `p.stdin.write( ("%s %s\n"%(xx1,xx2) ).encode() )` `p.stdin.close() ` `#include ` `int main()` `{` `double x1,x2;` `scanf("%lf", &x1); scanf("%lf", &x2);` `printf( "\n %.1lf + %.1lf = %.1lf\n", x1, x2, x1+x2 );` `return 0;` `}` This variant is not specific for an external program just in C language. – Serge Jan 02 '17 at 16:52
  • Another variant, using arguments of main() function in C part, looks even more simple: ****: `import subprocess` `xx1 = 1.; xx2 = 2.` `p = subprocess.Popen( "a.exe %f %f" % ( xx1, xx2 ) )` ****: `#include ` `int main(int argc, char *argv[])` `{` `double x1,x2;` `sscanf(argv[1],"%lf",&x1);` `sscanf(argv[2],"%lf",&x2);` `printf( "\n%.1lf + %.1lf = %.1lf\n", x1, x2, x1+x2 );` `return 0;` `}` – Serge Jan 02 '17 at 16:59
  • I reopened this question so you can post those solutions in a proper answer. The old dupe target was http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – PM 2Ring Jan 03 '17 at 01:08

0 Answers0