|
From: Jason H. <jas...@bt...> - 2001-11-23 02:36:03
|
You appear to not understand what argc and argv are. They are a standard =
part of a C/C++ program with a main() function and are linked together =
in thier use. When you execute a program and follow it with various =
parameters, i.e.:
MyProg -a 20 -b MyFile.foo
then the OS breaks the string down into each of the individual words =
(seperated by spaces (usuualy)) and uses both argc and argv together to =
make them accessible to the program. The first argc is pretty simple, it =
gives you the number of arguments passed to your program including the =
string representing the executable command. So in the above example argc =
would be 5. Note it depends on the OS as to how the command is broken =
down (AFAIK), but usualy spaces are it, possibly with commands enclosed =
in qoutes treated as one command and kept whole.
The second argv is a little more complex. I hope you are OK with =
pointers, because we are dealing with a pointer, to a pointer, to a =
string. Or if you prefer an array of pointers to strings. Basically you =
have an array of values that point to all the command strings passed. =
The OS doesn't convert numbers for you (as you seem to think) it just =
passes everything as strings. The above command would translate into:
"MyProg", "-a", "20", "-b", "MyFile.foo"
If you wanted to display the name of the program then you could use:
cout << argv[0];
or
cout << *argv;
The array version is used more often as it is easier to understand when =
seen. The fifth argument could be displayed with:
cout << argv[4];
Displaying: MyFile.foo
Using this method the OS can allow any kinds of arguments to be passed =
into a program without worrying about what is right or wrong. It leaves =
it up to you, the programmer, to detect right and wrong.
Here is a simple example (untested):
// Add.exe
#include <iostream>
#include <cstdlib>
#include <cctype>
int main(int argc, char *argv[])
{
// make sure enough args supplied
if(argc !=3D 3)
{
cout << "Usage: " << argv[0] << " A B" << endl;
cout << "Where A and B are integers" << endl;
exit(1);
}
// make sure both values are integers
char *pch;
// first arg
for(pch =3D argv[1]; pch; ++pch)
{
if(!isdigit(*pch))
{
cout << "Values must be integers!" << endl;
exit(2);
}
}
// second arg
for(pch =3D argv[2]; pch; ++pch)
{
if(!isdigit(*pch))
{
cout << "Values must be integers!" << endl;
exit(2);
}
}
// add the numbers together and output
cout << argv[1] << " + " << argv[2] << " =3D ";
cout << atoi(argv[1]) + atoi(argv[2]) << endl;
=20
return 0; // all ok
}
Try compiling that, correct any syntax or other errors I may have made, =
and it should when executed from a dos prompt output the sum of the two =
digits supplied.
If you are a newbie then I don't expect you to understand all that I did =
in that. I'm a bit lazy and didn't want to write it in a way that was =
easier to read to someone not used to pointers etc... (I hope it works =
now).
I hope this helps, Jason.
PS: when I say strings I was refering to the C style of null terminated =
array of chars, and not the container class in C++.
----- Original Message -----=20
From: Joh...@ao...=20
To: dev...@li...=20
Sent: Friday, November 23, 2001 1:46 AM
Subject: [Dev-C++] (no subject)
#include <iostream.h>
#include <stdio.h>
//QUESTION: Using what feature on the compiler to enter the 'argv'?
void main(int argc, char** argv)
{
int a =3D 10;
int b =3D a + argc;
cout<<" b =3D a + argc: "<<b<<endl;
int d =3D a + int(argv);//
cout<<" d =3D a + argv: "<< d<<endl;
getchar();
}
//I enter the 'argc' through the input of 'Parameter' Button then hit
//the 'Exection' to run the program. But I dont know how/where to =
enter the
//'argv' through the command line.....
Very appreciate your help..........=20
|