E.4. Using Command-Line Arguments

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.


Категории