On many systemsWindows, UNIX, LINUX and Mac OS X in particularit is possible to pass arguments to main from a command line by including parameters int argc and char *argv[] in the parameter list of main. Parameter argc receives the number of command-line arguments. Parameter argv is an array of char *'s pointing to strings in which the actual command-line arguments are stored. Common uses of command-line arguments include printing the arguments, passing options to a program and passing filenames to a program.
Figure E.3 copies a file into another file one character at a time. The executable file for the program is called copyFile (i.e., the executable name for the file). A typical command line for the copyFile program on a UNIX system is
$ copyFile input output
Figure E.3. Using command-line arguments.
(This item is displayed on pages 1252 - 1253 in the print version)
1 // Fig. E.3: figE_03.cpp 2 // Using command-line arguments 3 #include 4 using std::cout; 5 using std::endl; 6 using std::ios; 7 8 #include 9 using std::ifstream; 10 using std::ofstream; 11 12 int main( int argc, char *argv[] )l 13 { 14 // check number of command-line arguments 15 if ( argc != 3 ) 16 cout << "Usage: copyFile infile_name outfile_name" << endl; 17 else 18 { 19 ifstream inFile( argv[ 1 ], ios::in ); 20 21 // input file could not be opened 22 if ( !inFile ) 23 { 24 cout << argv[ 1 ] << " could not be opened" << endl; 25 return -1; 26 } // end if 27 28 ofstream outFile( argv[ 2 ], ios::out ); 29 30 // output file could not be opened 31 if ( !outFile ) 32 { 33 cout << argv[ 2 ] << " could not be opened" << endl; 34 inFile.close(); 35 return -2; 36 } // end if 37 38 char c = inFile.get(); // read first character 39 40 while ( inFile ) 41 { 42 outFile.put( c ); // output character 43 c = inFile.get(); // read next character 44 } // end while 45 } // end else 46 47 return 0; 48 } // end main |
This command line indicates that file input is to be copied to file output. When the program executes, if argc is not 3 (copyFile counts as one of the arguments), the program prints an error message (line 16). Otherwise, array argv contains the strings "copyFile", "input" and "output". The second and third arguments on the command line are used as file names by the program. The files are opened by creating ifstream object inFile and ofstream object outFile (lines 19 and 28). If both files are opened successfully, characters are read from file input with member function get and written to file output with member function put until the end-of-file indicator for file input is set (lines 4044). Then the program terminates. The result is an exact copy of file input. Note that not all computer systems support command-line arguments as easily as UNIX, LINUX, Mac OS X and Windows. Some VMS and older Macintosh systems, for example, require special settings for processing command-line arguments. See the manuals for your system for more information on command-line arguments.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography