Reading command line parameters when main does not pass them??

Q

qazmlp

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().
}
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

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.
 
V

Victor Bazarov

qazmlp said:
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.
}
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
 
J

JKop

qazmlp posted:
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
 
A

Adrian

Victor said:
qazmlp said:
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
 
V

Victor Bazarov

Adrian said:
Victor said:
qazmlp said:
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?!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top