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.
// 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 0;
}
// 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):
cpp_file = ''
exe_file = ''
opts, args = getopt.getopt(argv,
"i:",['ifile='])
for o, a in opts:
if o in ("-i", "--ifile"):
cpp_file = a + '.cpp'
exe_file = a + '.exe'
run(cpp_file, exe_file)
def run(cpp_file, exe_file):
print("Compiling " + cpp_file)
os.system('g++ ' + cpp_file + ' -o ' +
exe_file)
print("Running " + exe_file)
print("-------------------------")
print
os.system(exe_file)
print
if __name__ =='__main__':
main(sys.argv[1:])
C:\Program Files\OpenOffice 4\program>Python
c:\pyprg\main.py -i c:\pyprg\hello [('-i', 'c:\\pyprg\\hello')]
Compiling c:\pyprg\hello.cpp
c:\pyprg\hello.cpp: In function 'int main()': c:\pyprg\hello.cpp:6:21: error:
expected ';' before 'return'
std::cout<<"hello"
^
;
return 0;
~~~~~~
Running c:\pyprg\ch14\cpp_file.exe
-------------------
'c:\pyprg\hello.exe' is not recognized as an
internal or external command, operable program or batch file.
C:\Program Files\OpenOffice 4\program>
Note
In the above program Python helps to display
the error in C++. The error is displayed along with its line number. The line
number starts from python script.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.