C++ - Command line arguments - How to send an error message if there are no command line arguments given?

Hi, guys!

The title of the topic pretty says it all - how to send an error message if there are no command line arguments given? Allow me to explain to what I'm trying to do.

I would like to create an executable called "Launcher" launching another executable called "Engine". Now, I do not want the user to launch "Engine" without starting "Launcher" first, because "Launcher" gives brief instructions for "Engine" how it should operate. If there are no command line arguments given, error message should pop up saying: "
"Engine" cannot start without "Launcher".
"

So, how can this be done?
if (argc<2) cerr<<"\"Engine\" cannot start without \"Launcher\".", return 1;
Thank you viliml! Simple yet satisfying!
if there are no command line argument give than argc = 0.
i
1
2
f(argc == 0)
 // give error "Engine" cannot start without "Launcher". 
I thought one argument was always passed, the local path to the executable?
It is. I don't believe argc can ever be less than 1.
HiteshVaghani1 wrote:
if there are no command line argument give than argc = 0.

Nope, there is always one argument that is mostly, or always(not really sure) equal to the macro __FILE__
Another way will be using environment variables -> set them in Launcher, then execute Engine; all environment variables will be kept and no other processes can see them (command line arguments can be easily read by external tools, like task manager).
Last edited on
__FILE__ expands to the name of the current code file.
Normally argv[0] contains the name that was used to start the program. The standard also allows argv[0] to be "" or argc to be 0.
Last edited on
@viliml: sorry i made mistake in hurry...
Topic archived. No new replies allowed.