Fedora Linux Support Community & Resources Center
Sections ›› Home | Forums | Guidelines | Forum Help | Fedora FAQ | Fedora News 

Go Back   FedoraForum.org > The Community Lounge > Programming

Programming A place to discuss programming and development

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 2009-11-01, 02:12 PM CST
thelimpkid Online
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Posts: 49
linuxfedorafirefox
Command-line arguments in C++

I'm trying to use command-line arguments in an efficient way.

When I tried:
Code:
int main(int argc, char* argv)
{
	if (argv[1] == "new")
	{
		cout << "Something New!\n";
	}
	return 0;
}
I wasn't able to get "Something New!" to print on the screen.

However, when I tried:
Code:
int main(int argc, char* argv)
{
	string cla;
	cla = argv[1];
	if(cla == "new")
	{
		cout << "Something New!\n";
	}
	return 0;
}
I was able to get "Something New!" to print on the screen.

Why didn't it work on the first piece of code?
Reply With Quote
  #2  
Old 2009-11-01, 05:38 PM CST
drago01 Offline
Registered User
 
Join Date: Nov 2009
Posts: 5
linuxfedorafirefox
Because you where comparing a pointer to const char * which could not match, in the second case you are creating a string object and using an overloaded operator for comparison which works exactly this way.
Reply With Quote
  #3  
Old 2009-11-01, 06:14 PM CST
Jon_Morehouse Offline
Registered User
 
Join Date: Oct 2008
Location: NB, Canada
Posts: 24
linuxfedorafirefox
Just to add to drag01's response: In your first case you are asking to compare the string "new" with the contents of argv[1], which is only the character 'n'.

In your second piece of code you are accessing the string capabilities of C++, which know how to deal with an operation (from the comparison operator ==) for a string. Therefore, it reads to the end of the string, stopping when it hits NULL.
Reply With Quote
  #4  
Old 2009-11-01, 07:23 PM CST
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 2,597
linuxfirefox
Alternatively, you could do this:
Code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
	if (strcmp(argv[1],"new") == 0)
	{
		cout << "Something New!\n";
	}
	return 0;
}
Reply With Quote
  #5  
Old 2009-11-02, 09:01 AM CST
thelimpkid Online
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Posts: 49
linuxfedorafirefox
Thanks for the explanation. It seems so obvious now.
However, I can't get strcmp() to work, even if I include <string>.

Well, it doesn't matter anyway, I want to do it without using a string, and without using a for loop.
What is the best way to capture the command-line arguments?

Last edited by thelimpkid; 2009-11-02 at 04:17 PM CST.
Reply With Quote
  #6  
Old 2009-11-02, 03:20 PM CST
drago01 Offline
Registered User
 
Join Date: Nov 2009
Posts: 5
linuxfedorafirefox
To use the str* functions include cstring

Quote:
What is the best way to capture the command-line arguments?
Quote:
man 3 getopt
Reply With Quote
  #7  
Old 2009-11-02, 04:23 PM CST
thelimpkid Online
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Posts: 49
linuxfedorafirefox
getopt() seems like another function I have missed in my book.
Is it just as efficient as using a c-style string?
Reply With Quote
  #8  
Old 2009-11-03, 06:05 AM CST
TheCodeTwister Offline
Registered User
 
Join Date: Oct 2009
Posts: 3
linuxfedorafirefox
In case of C++, I would prefer:
http://www.boost.org/doc/libs/1_40_0...m_options.html

See the provided example for explanations.
Reply With Quote
  #9  
Old 2009-11-03, 08:15 AM CST
thelimpkid Online
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Posts: 49
linuxfedorafirefox
Quote:
Originally Posted by TheCodeTwister View Post
In case of C++, I would prefer:
http://www.boost.org/doc/libs/1_40_0...m_options.html

See the provided example for explanations.
I think it's quite unnecessary to use an external library for such a simple task. But thanks for pointing into another direction.

I think from what I've seen now is that a c-style string would work in the most efficient way..

Thanks for all the help!
Reply With Quote
  #10  
Old 2009-11-14, 11:59 PM CST
jonduf Offline
Registered User
 
Join Date: Dec 2006
Posts: 57
linuxfedoraepiphany
Quote:
Originally Posted by thelimpkid View Post
I think it's quite unnecessary to use an external library for such a simple task. But thanks for pointing into another direction.

I think from what I've seen now is that a c-style string would work in the most efficient way..

Thanks for all the help!
In your example maybe, but as the number of options grow and the complexity of the arguments grow, it is best to use a well understand and well tested library that is designed to handle command line args properly. Plus it leaves you the opportunity to code the meat and potatoes of your program.
Reply With Quote
  #11  
Old 2009-11-15, 01:30 PM CST
thelimpkid Online
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Posts: 49
linuxfedorafirefox
Quote:
Originally Posted by jonduf View Post
In your example maybe, but as the number of options grow and the complexity of the arguments grow, it is best to use a well understand and well tested library that is designed to handle command line args properly. Plus it leaves you the opportunity to code the meat and potatoes of your program.
I noticed indeed that it can be handy to use a well tested library when the number of options grow. In terms of performance it doesn't matter at all I guess, so after all it doesn't seem so useless to use an external library at all.
Reply With Quote
  #12  
Old 2009-11-17, 07:22 AM CST
mohan.10 Offline
Registered User
 
Join Date: Sep 2008
Location: India
Posts: 59
linuxsusefirefox
Smile

Quote:
Originally Posted by thelimpkid View Post
I'm trying to use command-line arguments in an efficient way.

When I tried:
Code:
int main(int argc, char* argv)
{
	if (argv[1] == "new")
	{
		cout << "Something New!\n";
	}
	return 0;
}
I wasn't able to get "Something New!" to print on the screen.

However, when I tried:
Code:
int main(int argc, char* argv)
{
	string cla;
	cla = argv[1];
	if(cla == "new")
	{
		cout << "Something New!\n";
	}
	return 0;
}
I was able to get "Something New!" to print on the screen.

Why didn't it work on the first piece of code?
Hey here are the exact lines from the book i'm referring to learn c++..

"Testing how C++ compares quoted strings is more difficult. Instead of using the contents
of the string, the compiler uses the location of the strings in memory, which is a detail of the
compiler’s internal workings and has no bearing on anything practical. Thus, unless you know
how the compiler works, you cannot predict how it will compare two quoted strings. In other
words, don’t do that. Make sure you create
objects before you compare strings."

It seems you are learning c++ too..I can suggest "Exploring C++(Apress)"..
Reply With Quote
Reply

Tags
c++, command-line arguments

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
wide character command line arguments a_user Programming 0 2008-03-02 07:08 AM CST
Bash: passing arguments to a command with a variable NeoNecro Programming 17 2007-01-30 11:20 AM CST
Add to text file from command line via echo... possible to add to aparticular line?[Scanned] Chris Bradford gmane.linux.redhat.fedora.general 11 2006-06-29 03:50 PM CDT
Is there a Command Line command to monitor disk read/write io rate to a particular hard drive? Leland T. Snyder alt.os.linux.redhat 3 2004-06-14 08:26 PM CDT
Re: Is there a Command Line command to monitor disk read/write io rate to x86processor alt.os.linux.redhat 2 2004-06-14 08:26 PM CDT

Automatic Translations (Powered by Powered by Google):
Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Taiwanese Thai Turkish Ukrainian Vietnamese Yiddish

All times are GMT -7. The time now is 06:31 AM CST.

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
Hosting provided by ThePlanet



All trademarks, and forum posts in this site are property of their respective owner(s).

FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact | Founding Members
Designed By Ewdison Then | Powered by vBulletin ©2000-2009, Jelsoft Enterprises Ltd.
FedoraForum is Powered by Open Source Projects and Products
Multi-language community supported by vBET Translator