12.3 Command-Line Arguments


12.3 Command-Line Arguments

Aside from reading and writing data to and from files, there is a final method of passing data to a C++ program-through command-line arguments. As mentioned, the main function is where execution for a program begins and ends. During the course of its execution other user-defined functions might be called and so on, but the ultimate beginning of an application and the end is within the main function. Because of this, the main function isn't invoked like other functions in the sense that you cannot call it, nor can you pass arguments to it with any real value. By virtue of the fact the main function is active, the program must be running. The main function can, however, accept two arguments. And since it is the operating system that causes the application to run by invoking main, it is therefore the operating system that can also pass those arguments to the main function. Thus, command-line arguments are good for setting an application to its initial behavior because they are arguments a program can immediately read as it begins. For example, they are often used to set programs to run in safe mode, to default to specific resolutions, or to run in windowed mode as opposed to full screen.

Command-line arguments work by being passed to applications on the command line, which is a text-based method for launching an application. If you open an application's context menu from its shortcut on the Windows Start menu or desktop background and then click Properties, you will see that a path is specified for Target, perhaps something like "C:\MyGame\Game.exe." This is a command line. A command-line argument therefore would be "C:\MyGame\Game.exe argument1." In fact, any words appearing in the file path and separated by a space are command-line arguments, which are strings passed to the main function.

The main function handles command-line arguments in the following form:

      int main (int argc, char* argv[]) 

Here there are two arguments, called argc and argv. Argc is the number of command-line arguments the function can expect to find in total. For argv there are as many array elements as there are command-line arguments, where each element of the array is a string representing the command-line argument. Consider the following:

      int main(int argc, char *argv[])      {         std::cout<<argv[0];         return 0;      } 
Note 

Argv is an array of strings (char*), not an array of characters. Essentially, it is an array of an array of characters because each element of argv is not a character, but an array of characters. That is to say, a string.




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net