delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/27/07:31:07

From: luke AT jet DOT msk DOT su (Oleg Yu. Polyanski)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C command line options
Date: 27 May 1998 14:51:15 +0400
Organization: Jet Infosystems
Lines: 44
Message-ID: <huu90noasws.fsf@goliath.jet.msk.su>
References: <CyQa1.113$mR4 DOT 91692 AT news DOT internetmci DOT com>
NNTP-Posting-Host: goliath.service.jet.msk.su
Mime-Version: 1.0 (generated by tm-edit 7.108)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

>>>>> "Clint" == Clint Allen <clint DOT allen AT mci2000 DOT com> writes:

	if you have ever used pascal, you should forget about it.
to be seriously, == operator will never compare strings, use function
`strcmp' instead, for example:

	if (strcmp (string, "kuku") == 0) /* string matches to kuku */
		do_something ();

your code just compares two pointers to type `char' and because they 
will never equal (argv is passed into the function on the stack but all
your strings ("/nts", "/?") are placed right in the code section, so
addresses of argv[1] and of "/nts" will always differ), comparison will
always fail. you may be intrested also in function `getopt', that 
automatically parses command line for you in a such way:

	char argument [ARRAY_OF_ENOUGH_SIZE];
	while ((ch = getopt (argc, argv, "ab:")) != EOF) {
		switch (ch) {
			case 'a':
				do_something_a ();
				break;
			case 'b':
				strcpy (argument, optarg);
				break;
		}
	}

type in the command prompt/favourite shell `info libc alpha getopt'
for details.

Clint> Anyone who can help on this?  I am trying to use command line
Clint> options in my program.  Here's what I've got so far:

Clint> int main(int argc, char* argv[]) { // Declare/init. variables
Clint> int title_on;

Clint> // Command line option procedure if (argc == 1) { title_on = 1;
Clint> } else { if (argv[1] == "/nts") title_on = 0; else if (argv[1]
Clint> == "/?")  { puts("Command line options");
Clint> puts("-----------------------"); puts("/?  Display this list");
Clint> puts("/nts No title screen"); return 0; } else { puts("Invalid
Clint> command line option.  Use \"/?\" to see valid options.");
Clint> return 0; } }

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019