Communication With the Outside World Program Return Code Arguments From the Program Call Aborting Program Calling Other Programs Data Processing Course, I. Hrivnacova, IPN Orsay I. Hrivnacova @ Data Processing Course 2019 1
Reminder: C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ // print Hello, World! on the screen std::cout << Hello, World! << std::endl; The main function called at the program startup, leaving main() ends the program I. Hrivnacova @ Data Processing Course 2018 2
Reminder: Function main() The function main is called at the program startup, leaving main() ends the program The function head: int the return type main the function name () the parameter list The function main() returns integer value If return statement is not present, the zero value is returned implicitly You can also write the statement explicitly: return 0; In C programs the return statement cannot be omitted I. Hrivnacova @ Data Processing Course 2018 3
Program Return Code // testhello1 cout << "Hello!" << endl; // testhello2 cout << "Hello!" << endl; return 1; The program return code can be accessed by a special shell variable $? >./testhello1 Hello! > echo $? 0 >./testhello2 Hello! > echo $? 1 I. Hrivnacova @ Data Processing Course 2018 4
Function main() with Parameters Function main can be declared in two ways: Without parameters: With two parameters:... int main(int argc, char* argv[])... I. Hrivnacova @ Data Processing Course 2018 5
Arguments from the Program Call argv[] : an array of C-strings, it contains the arguments from the call of the program The array always contains the program name as the first element (argv[0]) The other elements are program parameters (if any) argc: the number of elements in the argv array The concept is adopted from C, that s why C++ types are not used here int main(int argc, char* argv[])... I. Hrivnacova @ Data Processing Course 2018 6
#include <string> Arguments from the Program Call int main(int argc, char* argv[]) // output program name and number of parameters string programname = argv[0]; cout << "Program " << programname << endl; // output number of parameters if ( argc > 1 ) cout << "Called with " << argc -1 << " parameters" << endl; else cout << "Called without parameters" << endl; // output parameters for (int i = 1; i < argc; ++i) cout << "argv[" << i <<"]: " << argv[i] << endl; I. Hrivnacova @ Data Processing Course 2018 7
#include <string> Arguments from the Program Call int main(int argc, char* argv[]) // output program name and number of parameters string programname = argv[0]; cout << "Program " << programname << endl; // output number of parameters if ( argc > 1 ) cout << "Called with " << argc -1 << " parameters" << endl; else cout << "Called without parameters" << endl; // output parameters for (int i = 1; i < argc; ++i) cout << "argv[" << i <<"]: " << argv[i] << endl; Program outputs: >./testargv Program./testArgv Called without parameters./testargv alpha 10 beta Program./testArgv Called with 3 parameters argv[1]: alpha argv[2]: 10 argv[3]: beta I. Hrivnacova @ Data Processing Course 2018 8
Aborting Programs C++ programs is ended either by exiting main() Or by calling a function to abort a program Three functions are provided in <cstdlib> exit() atexit() abort() Interrupts a program in an orderly manner Installs a function that is called at the end of the program Interrupts a program in an disorderly manner I. Hrivnacova @ Data Processing Course 2018 9
std::exit() #include <cstdlib> // anywhere if ( fatal-problem ) std::exit(exit_failure); #include <cstdlib>... if ( error ) return EXIT_FAILURE;... When ending a program with exit() a not all objects are clean-up what can lead to undesirable side effect Calling exit() should be avoided when possible The int value received as an argument by exit() is passed to the caller of the program The constants EXIT_SUCCESS (0) and EXIT_FAILURE, defined in <cstdlib> can be also used as return values of main() I. Hrivnacova @ Data Processing Course 2018 10
std::abort() #include <cstdlib> // anywhere if ( fatal-problem ) std::abort(); // abort with core dump abort() is used to abort program immediately No additional cleaning-up The precise reaction is system specific A core dump is typically created I. Hrivnacova @ Data Processing Course 2018 11
Calling Other Programs Using system() function defined in <cstdlib> The name pf the program or the shell command is passed as C- string #include <cstdlib> system("ls -l"); system("/path/otherprogram"); I. Hrivnacova @ Data Processing Course 2018 12
Calling Other Programs - Example // testhello1 cout << "Hello World!" << endl; Program called #include <cstdlib> // testsystem cout << "Calling testhello1" << endl; Calling program system("./testhello1"); cout << "Back in testsystem" << endl; I. Hrivnacova @ Data Processing Course 2018 13
Calling Other Programs - Example // testhello1 cout << "Hello World!" << endl; #include <cstdlib> // testsystem cout << "Calling testhello1" << endl; system("./testhello1"); cout << "Back in testsystem" << endl; Program output: >./testsystem Calling testhello1 Hello World! Back in testsystem I. Hrivnacova @ Data Processing Course 2018 14