how do i accept command line parameters onto a program running under windows, i understand argv is the number of arguements but for eg - if i wanted to accept a 2 as a command line param and act on it - how do i do it.
tia
Printable View
how do i accept command line parameters onto a program running under windows, i understand argv is the number of arguements but for eg - if i wanted to accept a 2 as a command line param and act on it - how do i do it.
tia
i thought argc held the number of arguments where argv held the arguments ??
Welcome back!!!!!
I'm assuming you mean a Win32 Program that uses WinMain???
If so, the third param of WinMain is a LPSTR which will give you the command line as a string ..
Also, you can use the GetCommandLine() API at any point to get this data......
You need some sort of way to tokenise the commandline - so look at strtok from the Std C Library
That is correct. Except that argc will contain number of args + 1, and argv[0] will contain the name of the program.Quote:
i thought argc held the number of arguments where argv held the arguments ??
The data is in character format, so to extract a number, do:
int first_arg = atoi(argv[1]);
But always remember to check the count in argc, if it is 1, then nothing was entered into the command line, and the program should probably exit gracefully ;)
By the way, the naming convention is arbitrary, you could equally do:
int main(int count, char* array_of_commands[])
Code:#include <stdio.h>
int main(int argc, char *argv[])
{
while (--argc)
printf ("%s ", *++argv);
return 0;
}
/* OR */
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
puts(argv[i]);
return 0;
}