Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
inputfile.write("47");
cmd = ["gcc", "-O2", srcname, "-o", execname];
p = subprocess.Popen(cmd,stderr=errfile);
errfile.close();
#print(p)
p.wait();
...
subprocess.call(["./"+execname],stdin=inputfile,stdout=outputfile)


Above is some portion of my code to compile a C program using Python.

srcname is the input .c filename
execname is the C Executable filename

subprocess.Popen(cmd,stderr=errfile); // This is compiling my C program and printing the errors in errfile

subprocess.call(["./"+execname],stdin=inputfile,stdout=outputfile)// This is executing my C file on the condition that compilation is succesful

Below is my C source code in the file named srcname:

#include <stdio.h>
int main(int argc, char *argv[])
{
int n;
scanf("%d",&n);
printf("%d",n);
}


My inputfile contains 47 (refer first line). But the outputfile always contains 14.


using the below command line argument, I am getting right output in the output file.

C:/gcc srcname.c
C:/a.exe<input.txt>output.txt
Posted
Comments
Richard MacCutchan 20-Dec-14 4:45am    
You have not closed the input file after the write command, so it is quite possible that it has not flushed to disk.
875ewh84 20-Dec-14 5:06am    
Thanks. I has not closed the input file. I closed it and then opened it in read mode.

1 solution

Python
inputfile.write("47");
inputfile.flush();
cmd = ["gcc", "-O2", srcname, "-o", execname];
p = subprocess.Popen(cmd,stderr=errfile);
errfile.close();
#print(p)
p.wait();
...
subprocess.call(["./"+execname],stdin=inputfile,stdout=outputfile)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900