C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading command line parameters when main does not pass them

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
qazmlp
Guest





PostPosted: Tue Oct 26, 2004 7:58 pm    Post subject: Reading command line parameters when main does not pass them Reply with 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().
}
Back to top
Nils O. Selåsdal
Guest





PostPosted: Tue Oct 26, 2004 8:00 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote



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





PostPosted: Tue Oct 26, 2004 8:06 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote



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.

Quote:
}

V

Back to top
JKop
Guest





PostPosted: Tue Oct 26, 2004 8:20 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote

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





PostPosted: Tue Oct 26, 2004 10:10 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote

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





PostPosted: Tue Oct 26, 2004 10:25 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote

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 Smile

How in hell is it useful if it's empty?!

Back to top
Jacek Dziedzic
Guest





PostPosted: Wed Oct 27, 2004 10:37 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote

JKop wrote:
Quote:
How about:
[...]

What you have:
Quote:
int main( int argc, char* argv[] )

What the OP wanted:
Quote:
int main()

You can't access argv when because of "error: `argv' undeclared"
- J.

Back to top
Adrian
Guest





PostPosted: Sun Oct 31, 2004 5:04 pm    Post subject: Re: Reading command line parameters when main does not pass Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.