Thread: Alright, how does one compare an element of argv with a literal string?

  1. #31
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Programmer_P View Post
    Well, that is what I originally guessed was happening if I compare argv[3] with a literal string. However, when I attempted to dereference the pointer at element 3 of argv, it wouldn't compile, as I think I stated in an earlier post.
    That's because dereferencing the pointer means you are comparing the value of one character with the address in memory of another character. Such comparisons don't make sense, so compilers reject them.

    Quote Originally Posted by Programmer_P View Post
    Does the string::c_str() function work with comparison operators such as '!=' and '==', if I use those operators with the other operand being a literal c-style string?
    No. The result of string::c_str() is a pointer. If you use the comparison operators on it, you are doing a pointer comparison. If you want to compare the data that pointer points at with the data pointed at by another pointer (eg a string literal) then use strcmp().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #32
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Quote Originally Posted by Programmer_P View Post
    So how do I check then to see if the user entered quotes around the filepath?
    Why do you need to know that the user entered quotes? The quotes allow a parameter to have spaces embedded in it. eg "the big c++ program" will be one argument not the 4 it would be without the quotes.

  3. #33
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    That's because dereferencing the pointer means you are comparing the value of one character with the address in memory of another character. Such comparisons don't make sense, so compilers reject them.



    No. The result of string::c_str() is a pointer. If you use the comparison operators on it, you are doing a pointer comparison. If you want to compare the data that pointer points at with the data pointed at by another pointer (eg a string literal) then use strcmp().
    Thanks for that, but I'm already using strcmp() and that part of my code is working.

  4. #34
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    Why do you need to know that the user entered quotes? The quotes allow a parameter to have spaces embedded in it. eg "the big c++ program" will be one argument not the 4 it would be without the quotes.
    Exactly. I want to accept filenames which might have spaces in them.

  5. #35
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, since it's a char array, you can use the == operator on the first char of the parm element. That should make you happy!

    Code:
    if (argv[1][0] == '\"') { already a double quote.... }
    Mainframe assembler programmer by trade. C coder when I can.

  6. #36
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Dino View Post
    Well, since it's a char array, you can use the == operator on the first char of the parm element. That should make you happy!

    Code:
    if (argv[1][0] == '\"') { already a double quote.... }
    But I thought the quotes are already stripped from the arrays being pointed at by argv's elements?

  7. #37
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Programmer_P View Post
    That sucks...
    I was wanting to keep the quotes. I LIKE QUOTES!!!

    Thanks. So how do I check then to see if the user entered quotes?
    So, I tell you how. Then you say:

    Quote Originally Posted by Programmer_P View Post
    But I thought the quotes are already stripped from the arrays being pointed at by argv's elements?
    OMG!! Make up your friggin mind for which direction you are going with your conversation.

    C Runtime didn't change between these last few posts. Yes, they are removed. I gave you a sample of how to test for quotes, and I just happened to use argv[1] - but it is not impossible to pass quotes, but the outer quotes are removed by default.

    You don't need quotes to open a file. If you want quotes, add them back in. But take them out again if you plan on actually opening the file. Holy cow.
    Last edited by Dino; 05-30-2010 at 10:24 AM.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #38
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  9. #39
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes we do.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #40
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Dino View Post
    So, I tell you how. Then you say:



    OMG!! Make up your friggin mind for which direction you are going with your conversation.
    My mind didn't change direction. I said I wanted to keep the quotes, then I asked how to do that? Then when you responded with an if statement to check if the first character of the argv[1] is a double quote character, I questioned it because in my understanding, the quotes are already stripped from the array somehow. What's wrong with that?
    C Runtime didn't change between these last few posts. Yes, they are removed. I gave you a sample of how to test for quotes, and I just happened to use argv[1] - but it is not impossible to pass quotes, but the outer quotes are removed by default.
    So you're saying someone would have to pass double double quotes in order for one of them to stay?
    You don't need quotes to open a file. If you want quotes, add them back in. But take them out again if you plan on actually opening the file. Holy cow.
    It was my understanding that if I pass quotes in the filepath I pass to ifstream:pen, then a file with spaces in the filename can open. Otherwise it can't. Is this incorrect?

  11. #41
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Programmer_P View Post
    So you're saying someone would have to pass double double quotes in order for one of them to stay?
    Try it and you tell me.

    Quote Originally Posted by Programmer_P View Post
    It was my understanding that if I pass quotes in the filepath I pass to ifstream:pen, then a file with spaces in the filename can open. Otherwise it can't. Is this incorrect?
    A C-string is passed - blanks or any other characters don't have anything to do with it - it's all about the null terminator ending the file name.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #42
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Programmer_P View Post
    Quick question though:

    Does the string::c_str() function work with comparison operators such as '!=' and '==', if I use those operators with the other operand being a literal c-style string?
    Well yes, but it won't do what you expect it to do. As mentioned earlier, an array of char's is actually a pointer. The c_string() function in std::string returns a const pointer to an array of chars, so if you do something like this:

    Code:
    if(myStdString.c_str() == "--help")
    ...you will NOT be comparing the contents of the std::string with the constant of the C-style string, you will be comparing the MEMORY ADDRESS of the two. The pointer returned by the c_str() function might point to a random address like 0x00241 and the "==" operator will then check if the "-help" string also resides in 0x00241, which it ofcourse does not.

    What you WANT to do is this:
    Code:
    #include <cstring>
    ...
    if(std::strcmp(myStdString.c_str(), "--help") )
    This will compare the CONTENTS of the strings, not the memory address, and i think this is what you really want.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  13. #43
    Registered User
    Join Date
    May 2010
    Posts
    9
    Yeah this you can do by using"operator overloading" for which you need to write a function

  14. #44
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Neo1 View Post
    Well yes, but it won't do what you expect it to do. As mentioned earlier, an array of char's is actually a pointer. The c_string() function in std::string returns a const pointer to an array of chars, so if you do something like this:

    Code:
    if(myStdString.c_str() == "--help")
    ...you will NOT be comparing the contents of the std::string with the constant of the C-style string, you will be comparing the MEMORY ADDRESS of the two. The pointer returned by the c_str() function might point to a random address like 0x00241 and the "==" operator will then check if the "-help" string also resides in 0x00241, which it ofcourse does not.

    What you WANT to do is this:
    Code:
    #include <cstring>
    ...
    if(std::strcmp(myStdString.c_str(), "--help") )
    This will compare the CONTENTS of the strings, not the memory address, and i think this is what you really want.
    I thank you, but...
    How many times are people going to answer that question??
    We've been through all this already. For the last time, I now know you must always use strcmp() to compare two c-style strings, and if you want to compare a C++ string with a c-style string, then you just pass to strcmp the c_str() function of the string in one of the parameters. I get it now. I really do..
    I'm way past that problem now.

  15. #45
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Is there anyway I can mark this thread as solved?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM