Compile a C/C++ Program and store standard output in a File via Python

2024/10/5 17:28:28

Let's say I have a C/C++ file named userfile.c.

Using Python, how can I invoke the local gcc compiler so that the file is compiled and an executable is made? More specifically, I would like to provide some input (stdin) via some file input.txt and I want to save the standard output into another file called output.txt. I saw some documentation that I will need to use subprocess, but I'm not sure how to call that and how to provide custom input.

Answer

A simple solution will be as given below:

import subprocessif subprocess.call(["gcc", "test.c"]) == 0:subprocess.call(["./a.out <input.txt >output.txt"], shell=True)
else: print "Compilation errors"

2 caveats:

  1. I am hardcoding stuff. You may want to parameterize and all that.
  2. Setting shell to True, is a security risk, per Python documentation.
https://en.xdnf.cn/q/119908.html

Related Q&A

How to swap maximums with the minimums? (python)

Is there a method to swap the maximum and the minimum of a list? The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second …

python object attributes and methods

In python all data is object and any object should have attributes and methods. Does somebody know python object without any attributes and methods?>>> len(dir(1)) 64

How to retrieve nested data with BeautifulSoup?

I have the below webpage source: </li><li class="cl-static-search-result" title="BELLO HONDA ACCORD &quot;95 MIL MILLAS&quot;. REALMENTE COMO NUEVO"><a href=&…

applying onehotencoder on numpy array

I am applying OneHotEncoder on numpy array.Heres the codeprint X.shape, test_data.shape #gives 4100, 15) (410, 15) onehotencoder_1 = OneHotEncoder(categorical_features = [0, 3, 4, 5, 6, 8, 9, 11, 12]) …

How to delete temp folder data using python script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Save a list of objects on exit of pygame game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Trying to make loop for a function that stops after the result is lower than a certain value

Im taking a beginner python class and part of an exercise we were given was this:The point x with the property x= sin(x)−ax+ 30 is called a fixed point of the function f(x) = sin(x)−ax+ 30. It can b…

python url extract from html

I need python regex to extract urls from html, example html code :<a href=""http://a0c5e.site.it/r"" target=_blank><font color=#808080>MailUp</font></a> <…

Regex match each character at least once [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to cluster with K-means, when number of clusters and their sizes are known [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…