how to manipulate argc and argv

Hi,

I have a requirement in which I want to supply a few command line arguments to my application by default if these are not supplied by the user.

I feel one way to do is to add these options to argc and argv on init. But I don't have any clue on how to do it?

If any one can please tell me any other way to pass these arguments or How to add to argc and argv..

Thanks in advance and waiting eagerly for a response

Thanks
Codeman
[488 byte] By [Codeman] at [2007-11-19 6:38:59]
# 1 Re: how to manipulate argc and argv
Well, why manipulate the argv() or argc()? Easier would be just swiching the flags and on the default values internally, and only if a parameter for it is passed, change them. That would be easier:

int parse_argv ( int argc, char* argv[] )
{
int default = 3; // << Default value
if ( argc == 2 )
{
default = atoi(argv[1]);
}
else
{
printf("Not enough parameters. Default 3 will be used.\n");
}
return default;
}

int main ( int argc, char* argv[] )
{
int val = parse_argv(argc, argv);
// process...
printf("%d", val + 5);

return 0;
}

This code uses a default parameter 3 if the user specifies two numbers or none. That's what you meant...
NoHero at 2007-11-9 0:41:45 >
# 2 Re: how to manipulate argc and argv
You can't manipulate argc on init; it is an array of const char *, so the strings are not modifyable. Instead, why not do this:

int main(int argv, const char *argc[])
{
if (argv == 1) // no arguments were passed, only the program name
{
// apply default parameters here
}

else
{
for (int i = 1; i < argv; ++i)
{
// process command-line arguments here
}
}

return 0;
}
Bob Davis at 2007-11-9 0:42:43 >
# 3 Re: how to manipulate argc and argv
As u have said that is fine if i need only argc value. But in my case i need these default values to persist throughout the application.

Note this is a windows application.

i need to use these values in both Initinstance and ExitiInstance and also i need argv and argc both values not only argc.

Thanks
Codeman
Codeman at 2007-11-9 0:43:47 >
# 4 Re: how to manipulate argc and argv
You can't manipulate argc on init; it is an array of const char *, so the strings are not modifyable. Instead, why not do this:

Well ... if you specify that argv is char** you have pointers who lead somewhere on the heap. And I think this is manipulateable. But I would keep the fingers of it, it's dirty coding style and might cause bugs that are hard to find.
NoHero at 2007-11-9 0:44:44 >
# 5 Re: how to manipulate argc and argv
int main(int argv, const char *argc[])
:eek: Wow that's an interesting variance from conformity! The rest of the world use argc as the name for the int parameter - "argument count" and argv as the name for the char* array parameter - "argument value(s)". Or was it a very large typo??

T
Toot at 2007-11-9 0:45:51 >
# 6 Re: how to manipulate argc and argv
This thread describes how to get argc() and argv() from a Windows application by using Ansi and Unicode charset. (http://www.dev-archive.com/forum/showthread.php?t=330396&highlight=CommandLineToArgvW):

http://www.dev-archive.com/forum/showpost.php?p=1105362&postcount=10
http://www.dev-archive.com/forum/showpost.php?p=1105363&postcount=11
NoHero at 2007-11-9 0:46:50 >
# 7 Re: how to manipulate argc and argv
:eek: Wow that's an interesting variance from conformity! The rest of the world use argc as the name for the int parameter - "argument count" and argv as the name for the char* array parameter - "argument value(s)". Or was it a very large typo??

T

Typo. Far too early in the morning for me to be thinking. :)
Bob Davis at 2007-11-9 0:47:46 >