trying to use popen2() to communicate with C program

I. Myself No.Spam at Spam.none
Tue Mar 28 13:02:46 EST 2006


I can't get this to work:

# commer.py - to test communication with other process
from popen2 import popen2
(child_stdout, child_stdin) = popen2("commer.exe")
print "Got here 1"
line = child_stdout.readline()
print "Got here 2"
child_stdin.write(line)  
child_stdin.close
child_stdout.close

The compile C program, commer.exe, writes a line of text to its stdout.  
The python program does not receive it; it never gets to print "Got here 
2".   Commer.exe does begin execution.  Here's commer.c, but there 
appears to be no problem with it.  Commer.exe can be executed alone, and 
it behaves as expected.

/* commer.c - to test communication with other process */
/* this program will output a line of text to stdout, and then
   accept a line of input from stdin.
   It also creates a text file called out.txt and writes to it.
*/   
#include <stdio.h>
#include <stdlib.h>
int main()  {
    char line[] = "A line of text-----------------------------";
    FILE *outfile;
    /* Record that execution began: */
    outfile = fopen("out.txt", "w");
    fprintf(outfile, "commer.exe starting up.\n");
    fclose(outfile);
    /* Output a line of text to stdout: */
    printf("%s\n", line);
    /* Record that that happened: */
    outfile = fopen("out.txt", "a");
    fprintf(outfile, "text line was output to stdout\n");
    fclose(outfile);
    /* Get a line of text from stdin: */
    scanf("%s", line);
    /* Record the result: */   
    outfile = fopen("out.txt", "a");
    fprintf(outfile,"%s was received.\n", line);   
    fclose(outfile);
    return 0;
}

Thanks,

Mitchell Timin

-- 
I'm proud of http://ANNEvolve.sourceforge.net.  I'm currently working on a
major update of the SailChallenge package.  If you want to write software,
or articles, or do testing for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the number)
zenguy at shaw789 dot ca.





More information about the Python-list mailing list