By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

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

P: n/a
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().
}
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Tue, 26 Oct 2004 12:58:37 -0700, qazmlp wrote:
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.
int main()
{
func() ; // No command line arguments are passed to func().
}


Jul 22 '05 #2

P: n/a
qazmlp wrote:
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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
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.

"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

Jul 22 '05 #5

P: n/a
Adrian wrote:
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?!
Jul 22 '05 #6

P: n/a
JKop wrote:
How about:
[...]
What you have: int main( int argc, char* argv[] )


What the OP wanted:
int main()


You can't access argv when because of "error: `argv' undeclared"
- J.
Jul 22 '05 #7

P: n/a
Victor Bazarov wrote:
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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.