📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 268question

Refer installation of MinGW in Annexure -2 · Part 8

Chapter 4: 6 · COMPUTER SCIENCE

Chapter - - The Python script(program) is mainly used to read the C++ file along with the type of mode like ‘i’/’o’. ‘getopt()’ Parses(splits) each value of the command line and passes the options(values) as list to ‘opt’ and since no error ‘args’ generates empty list[]. Using ‘for loop’ the tuple in the list is unpacked - ‘o’ stores the mode and ‘a’ stores the name along with the path of the c++ file. The variable ‘ ’ store the c++ file along with its extension and ‘ ’ stores the executable file with .exe extension.

‘+’ usd in this program helps to concatenate the file name with the extensions. ‘os.system()’ along with ‘g++’ compiles the . Mode ‘o’ sends the executable file to ‘ ’. ‘ ’ variable directs the program to start from the beginning of the Python script(zero’th line) The “main()” definition does the Parsing and calling the run().

The “run()” invoke the “g++” compiler and creates the exe file. The system() of “os” module executes the .exe file and the desired output will be displayed on the output screen. The file extensions are added by the Python script so it is even possible to execute C programs. How Python is handling the errors in C++ .

Python not only execute the successful C++ program, it also helps to display even errors if any in C++ statement during compilation. For example in the following C++ program an error is there. Let us see what happens when you compile through Python. Example .

. // C++ program to print the message Hello //Now select File → New in Notepad and type the C++ program #include<iostream> using namespace std; int main() { std::cout<<"hello" return ; } // Save this file as hello.cpp # Now select File → New in Notepad and type the Python program as main.py # Program that compiles and executes a .cpp file # Python main.py -i hello import sys, os, getopt def main(argv): opts, args = getopt.getopt(argv, "i:") for o, a in opts: if o in "-i": run(a) 12th Computer Chapter - -

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →