473,788 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2320
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. GetModuleFileNa me 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 SomeOtherFuncti on()
{
//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
5378
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 manuals for the proper techniques to prepare an sqlj program. When I went to try the sqlj command, I got this: Exception in thread "main" java.lang.NoClassDefFoundError: sqlj/tools/Sqlj
6
2937
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
7
1585
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 it should look up man - find that its a function, run the man function, and then, with that second word "something" it should then execute that as its parameters, if that make sense. At the moment, it semi works, i.e If i type man it will run...
1
2218
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: C:\MyProgam.exe "C:\MyFile.txt" I have been able to process the command line by writing a small sub main in a module and that works fine. But I need for their to be only 1 instance of the program. As it is now it opens a new instance of MyProgram.exe...
4
15079
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 this program returns. I'm just missing the stepshere. I know that I can set the Shell command to an integer,but this only returns a 0 to me telling me that it executed, notwhat is being returned to the console by that application. Isthere a way to...
16
2724
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 names of Linux packages to install. Running the script as above will create this line: sudo aptitude install package1 package2 ...
10
8358
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 delivery of Travis Oliphant's NumPy manual, I have a quick question (hopefully) regarding how to read in Fortran written data. The data files are not binary, but ASCII text files with no formatting and mixed data types (strings, integers,...
3
4829
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 declare them in Program.cs and access them from Form1.cs ? Pia
2
4247
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 question is how to elegantly pass a command line parameter from Instance_B to Instance_A where Instance_A was running prior to Instance_B. For example, the user can launch the program by passing a file name as a command line argument. The program...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.