472,378 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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().
}
Jul 22 '05 #1
7 2177
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Rhino | last post by:
I am having an odd problem: the sqlj command on my system doesn't work. I am running DB2 (LUW) V8 (FP8) on WinXP. I haven't done an sqlj program since Version 6 of DB2 (LUW) so I checked the...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
7
by: Advocated | last post by:
Hey all, thanks for taking the time to read this in the first place. Anyway, ill try and keep it simple. In my program, if i type $ man something it should read in the 2 words, man and something...
1
by: slonocode | last post by:
I have created a filter in Eudora email program that will notify a program when the criteria is met. For instance if the filter criteria is met it will send the following command: ...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
16
by: John Salerno | last post by:
Here's my new project: I want to write a little script that I can type at the terminal like this: $ scriptname package1 where scriptname is my module name and any subsequent arguments are the...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
3
by: Pia Stevens | last post by:
For a GUI CSharp program I could define command line parameter as well by declaring them in Main(): static void Main(string args) But how do I pass them easily to Form1 ? Or should I...
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.