Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments

2 10-2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments – Arguments can be passed to any program being executed from a command-line based OS like DOS or UNIX – UNIX Example: cp fileA fileB – cp is the name of the program (command) to be executed – fileA and fileB are command-line arguments – In C/C++, the program can be written to accept command-line-arguments

3 10-3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C/C++ syntax – The following syntax is used in the main() function to capture command-line arguments int main(int argc, char *argv[]) – In this case, the operating system passes in two parameters to the function – argc is an integer specifying the number of command- line arguments – argv is an array of pointers where each pointer points to one of the command-line arguments stored as a C- type string

4 10-4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C++ syntax (Cont..) – In some systems (like UNIX), argv[0] points to a string that is the entire path to the program (executable) including its name – In other systems, argv[0] points to garbage – You can use any argv[i] like a C-type string – In C++, if the program is not written to accept command-line arguments, it is not necessary to specify the parameters in the argument list of the function signature

5 10-5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example #include using namespace std; void main(int argc, char *argv[]) { string fname, lname; fname = argv[1]; lname = argv[2]; cout << argc << " " << argv[1] << " " << argv[2] << endl; } g++ -o comlinarg comlinarg.cpp./comlinarg John Malkovich 3 John Malkovich


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments."

Similar presentations


Ads by Google