||Thread Tools||Display Modes|
|Mar 23rd, 2009, 8:03 PM||#1|
Join Date: Nov 2008
Rep Power: 9
C++ - Creating programs that utilize command line arguments.
This tutorial will teach you how to create programs that use command line arguments.
Terms to know:
1) Argument - A value that is to be used in a function.
2) Command line arguments - Arguments to be used in the "main()" function. Users type these arguments when calling a program via the command line interface. They are typed after the program name.
You've probably used arguments in your C++ programs. Many functions you create in your programs utilize arguments.
Here is an example of a program that uses arguments.
You only need to learn a few more rules to begin utilizing command line arguments.
1) Command line arguments are passed to the "main()" function when executing the program from the command line. They are typed after the program name.
2) Command line arguments are stored as an array of strings. In order to use non-string values as command line arguments, you will need to write code to convert the string argument to its intended numerical value.
3) The standard form of coding command line arguments is, "int main(int argc, char * argv)". "argc", and "argv" can be named any name that would be valid for a variable name, but "argc", and "argv" are the standard names. "argc" means argument counter. It is the number of arguments passed to the main function. "argv" means argument value. It is the value stored in the argument.
Note: Another form of coding command line arguments is, "int main(int argc, char ** argv)". The code with the single asterisk and the double asterisk perform the same function.
4) The program name is automatically "argv". User arguments start at "argv".
5) Arguments are separated by spaces.
Here is an example program that utilizes command line arguments. It will display the arguments the user types to the console.
To execute it, open your command line interface. (Usually cmd.exe in Windows. It can be opened using "Run..." on the Start menu.)
Change the working directory to the location of the built program, or copy the built program to the working directory. (On Windows, you can change the working director to a partition by typing "cd " without the quotes and the name of the partition. Usually it is "C:\". Example: "cd C:\" without the quotes.)
Type the name of the program, then type the words you wish to display.
Example: "program.exe Hello, world!" without the quotes.
The output for the arguments "Hello," and "world!" should be "Hello, world!".
Now you know how to use command line arguments.
|Mar 23rd, 2009, 10:21 PM||#2|
hoßßy¦st C++ Þrogrammer
Re: C++ - Creating programs that utilize command line arguments.
For Win32, there is a LPSTR (which is a char*) in place.
|Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)|
|Thread||Thread Starter||Forum||Replies||Last Post|
|Creating programs that can speak||PhilBon||Visual Basic .NET||2||Sep 10th, 2007 9:04 AM|
|Creating Windows Services/LocalSystem Programs||Baphomet||C++||1||Oct 19th, 2006 12:39 PM|
|the best way to pass data between two programs||nindoja||C#||7||Dec 26th, 2005 11:54 PM|
|Creating a spyware removal app||pizzaboy14x||Other Programming Languages||11||Sep 27th, 2005 8:48 PM|
|Comparing arguments to strings||layer||C++||2||May 7th, 2005 2:39 PM|