| View previous topic :: View next topic |
| Author |
Message |
qazmlp Guest
|
Posted: Tue Oct 26, 2004 7:58 pm Post subject: Reading command line parameters when main does not pass them |
|
|
void func()
{
// Is it by anyway possible to read the value of the first command
line parameter i.e. argv[0] here ?
}
int main()
{
func() ; // No command line arguments are passed to func().
}
|
|
| Back to top |
|
 |
Nils O. Selåsdal Guest
|
Posted: Tue Oct 26, 2004 8:00 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
On Tue, 26 Oct 2004 12:58:37 -0700, qazmlp wrote:
| Quote: | void func()
{
// Is it by anyway possible to read the value of the first command
line parameter i.e. argv[0] here ?
}
Not unless you use some ubermagic platform/implementation dependant ugly |
tricks.
| Quote: | int main()
{
func() ; // No command line arguments are passed to func().
}
|
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Oct 26, 2004 8:06 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
qazmlp wrote:
| Quote: | void func()
{
// Is it by anyway possible to read the value of the first command
line parameter i.e. argv[0] here ?
|
Not using standard C++ means. If you need to know the name of your
program (the executable that was used to start the process), some OSes
have some functions to retrieve that. E.g. GetModuleFileName in Win32.
| Quote: |
}
int main()
{
func() ; // No command line arguments are passed to func().
|
Nothing to pass. You used the form of 'main' without arguments. Even
if you did use 'main(int argc, char *argv[])' there is no guarantee that
argv[0] actually contains anything useful.
V
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Tue Oct 26, 2004 8:20 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
qazmlp posted:
| Quote: | void func()
{
// Is it by anyway possible to read the value of the first command
line parameter i.e. argv[0] here ?
}
int main()
{
func() ; // No command line arguments are passed to func().
}
|
How about:
namepace GlobalObjects
{
int argc;
char** argv;
}
int main( int argc, char* argv[] )
{
GlobalObjects::argc = argc;
GlobalObjects::argv = argv;
}
void SomeOtherFunction()
{
//Yippee! I have access to
//the command line args!
char* prog_name = GlobalObjects::argv[0];
}
If you want to pull them out of thin air, then Standard C++ provides no such
facility.
<OT>
If your platform is Win32, look-up "GetCommandLine"
</OT>
-JKop
|
|
| Back to top |
|
 |
Adrian Guest
|
Posted: Tue Oct 26, 2004 10:10 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
Victor Bazarov wrote:
| Quote: | qazmlp wrote:
int main()
{
func() ; // No command line arguments are passed to func().
Nothing to pass. You used the form of 'main' without arguments. Even
if you did use 'main(int argc, char *argv[])' there is no guarantee that
argv[0] actually contains anything useful.
"3.6.1.2 An implementation shall not predefine the main function. This |
function shall not be overloaded. It shall have a return type of type
int, but otherwise its type is implementation defined.
All implementations shall allow both of the following definitions of
main:int main() { /* ... */ }
and int main(int argc, char* argv[]) { /* ... */ }
In the latter form argc shall be the number of arguments passed to the
program from the environment in
which the program is run. If argc is nonzero these arguments shall be
supplied in argv[0] through
argv[argc1]
as pointers to the initial characters of null terminated
multibyte strings (NTMBSs)
(17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character
of a NTMBS that represents the name used to invoke the program or "".
The value of argc shall be nonnegative. The value of argv[argc] shall be
0. [Note: it is recommended that any further (optional) parameters be
added after argv. ]"
Not a guarantee of being passed the name of the exe but does say (to me
at least)that it will be the exe name or empty. Slightly more useful
then a chocolate tea pot :-)
Adrian
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Oct 26, 2004 10:25 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
Adrian wrote:
| Quote: | Victor Bazarov wrote:
qazmlp wrote:
int main()
{
func() ; // No command line arguments are passed to func().
Nothing to pass. You used the form of 'main' without arguments. Even
if you did use 'main(int argc, char *argv[])' there is no guarantee that
argv[0] actually contains anything useful.
[...]
Not a guarantee of being passed the name of the exe but does say (to me
at least)that it will be the exe name or empty. Slightly more useful
then a chocolate tea pot
|
How in hell is it useful if it's empty?!
|
|
| Back to top |
|
 |
Jacek Dziedzic Guest
|
Posted: Wed Oct 27, 2004 10:37 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
JKop wrote:
What you have:
| Quote: | int main( int argc, char* argv[] )
|
What the OP wanted:
You can't access argv when because of "error: `argv' undeclared"
- J.
|
|
| Back to top |
|
 |
Adrian Guest
|
Posted: Sun Oct 31, 2004 5:04 pm Post subject: Re: Reading command line parameters when main does not pass |
|
|
Victor Bazarov wrote:
| Quote: | Not a guarantee of being passed the name of the exe but does say (to
me at least)that it will be the exe name or empty. Slightly more
useful then a chocolate tea pot :-)
How in hell is it useful if it's empty?!
Hence the chocolate tea pot reference |
|
|
| Back to top |
|
 |
|