Communication With the Outside World

Similar documents
C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

primitive arrays v. vectors (1)

C++ Namespaces, Exceptions

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

PROGRAMMING IN C++ CVIČENÍ

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Introduction to C++: I

Programmazione. Prof. Marco Bertini

Pointers, Arrays and C-Strings

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

C-String Library Functions

The following program computes a Calculus value, the "trapezoidal approximation of

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Lecture 5 Files and Streams

Binghamton University. CS-211 Fall Variable Scope

Introduction. Lecture 5 Files and Streams FILE * FILE *

Topic 8: Lazy Evaluation

C++ Programming Fundamentals

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Arrays and Pointers (part 2) Be extra careful with pointers!

MPI 2. CSCI 4850/5850 High-Performance Computing Spring 2018

Arrays and Pointers (part 2) Be extra careful with pointers!

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Arrays. Week 4. Assylbek Jumagaliyev

Functions. Arizona State University 1

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

C++ Final Exam 2017/2018

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Strings and Stream I/O

Object-Oriented Programming, Iouliia Skliarova

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

2 2

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

MPI 3. CSCI 4850/5850 High-Performance Computing Spring 2018

Homework 4. Any questions?

Exercise 1.1 Hello world

My First Command-Line Program

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Physics 234: Computational Physics

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS2141 Software Development using C/C++ C++ Basics

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Data Structures Lab II. Binary Search Tree implementation

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 13. Functions and Parameter Passing (Part 2)

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

C Legacy Code Topics. Objectives. In this appendix you ll:

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Unified Modeling Language a case study

C++ Lab 02 - Command Line Arguments and Strings

Overview (1A) Young Won Lim 9/14/17

Lecture 12. We have already used strings. Strings. Hello Class is a string.

CSE 333 Lecture 9 - intro to C++

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

C:\Temp\Templates. Download This PDF From The Web Site

Binghamton University. CS-211 Fall Functions. The Basis of C

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Overview (1A) Young Won Lim 9/9/17

Streams. Rupesh Nasre.

C++ Programming Fundamentals

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Introduction to parallel computing concepts and technics

Functions and Recursion

Overview (1A) Young Won Lim 9/25/17

Name SECTION: 12:45 2:20. True or False (12 Points)

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Exceptions, Case Study-Exception handling in C++.

Intermediate Programming, Spring 2017*

CS201- Introduction to Programming Current Quizzes

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

C++ Programming Lecture 11 Functions Part I

Programming in C. Pointers and Arrays

Itron Riva Dev Software Development Getting Started Guide

Programming in C Lecture Tiina Niklander

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

A Unix Process. Joseph Cordina

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Exercise: Inventing Language

de facto standard C library Contains a bunch of header files and APIs to do various tasks

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Lab 1: Introduction to C++

Program template-smart-pointers-again.cc

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Exceptions. Why Exception Handling?

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Transcription:

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