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

This is a discussion on Alright, how does one compare an element of argv with a literal string? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Programmer_P Ok, another problem dealing with argv: When I get the argv[1] argument and store it in ...

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Programmer_P View Post
    Ok, another problem dealing with argv:

    When I get the argv[1] argument and store it in a string, I then get the size of the string by calling the size() function of that string.
    Later on in the code, I was noticing it was outputting the wrong message at was supposed to be the last character of the string (i.e. inputFilePathSize - 1). And so I went and outputted the size of the inputFilePath string (which is the string which was given the contents of argv[1]), and it turns out that size is 2 less than what it should be.

    It claims the length/size of the string is 32, when in reality, its 34. It also gets the size of argv[2] (an output filename string) wrong. It claims it has 7 characters, when in reality, it has 9.

    With both strings, I depending on the size() function of the string type, and store what that function returns inside an int. Is that the problem? I know that string::size() returns a size_t type which is an unsigned type as opposed to "int" which is a signed type. I don't know if that would change the inner value though. I was under the impression it wouldn't, which is why I'm using an int variable instead of a size_t for storing the size.
    And this problem still exists...
    My program will compile, but its not calculating the sizes of two C++ strings correctly, even though I use the string::size() function to get the sizes. It calculates the size of both as 2 less than what the size/length of the strings really are.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    No. The string::c_str() function returns a cstring. You then have to use strcmp().

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    No. The string::c_str() function returns a cstring. You then have to use strcmp().
    The problem is, that function can only compare two c-style strings. I want to compare a C++ string with a c-style string, without converting one to another first.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    How are you storing argv[1] to a string??

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No. The string::c_str() function returns a cstring. You then have to use strcmp().
    No. You see, when you type a literal C-string into your program, the compiler sets aside some memory for it in the .text segment (if you're using an Intel machine). Good compilers will make the same literal typed in different places in the program point to the same location in the .text segment. (Note that the .text segment is read-only.)

    But when you call .c_str() on a std::string, the code has to take a dynamically allocated string and represent it in C-string form. (On most compilers, this is probably already the internal representation, making this operation trivial.) The difference is that this will be a dynamically allocated object on the heap, not a string stored in the .data segment.

    When you use == or != on char*'s, you're just comparing the pointer. That is,
    Code:
    string == "data"
    checks whether the pointer stored in string is exactly the same pointer as the static .text segment pointer to "data". Since your string pointer is actually on the heap, this should never happen.

    The reason == works on std::strings is that the string class overloads the operator ==, to redefine what it does. For basic C-strings, you need to compare strings character by character to determine whether they are the same or not, which is what strcmp() does for you.

    [edit] I should mention that argv[] is most likely dynamically allocated as well (or perhaps stored on the heap), so comparing its elements with pointers like "--verbose" in the .text segment is unlikely to work as well. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    How are you storing argv[1] to a string??
    Good question.
    I'm storing it by assigning argv[1] to a string object called "inputFilePath". That's the same string I check later for its size (of which I store in an int variable), and which size is 2 less than what it should be.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Quote Originally Posted by Programmer_P View Post
    Good question.
    I'm storing it by assigning argv[1] to a string object called "inputFilePath". That's the same string I check later for its size (of which I store in an int variable), and which size is 2 less than what it should be.
    Could you please show the code?

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    Could you please show the code?
    Code:
    string inputFilePath = argv[1];

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Quote Originally Posted by Programmer_P View Post
    Code:
    string inputFilePath = argv[1];
    Ok and if argv[1] contains "--help" then inputFilePath.size() should equal 6. If it doesn't then

    a. What does it report?
    b. What does
    Code:
     std::cout << inputFilePath << std::endl;
    show?

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    Ok and if argv[1] contains "--help" then inputFilePath.size() should equal 6. If it doesn't then

    a. What does it report?
    b. What does
    Code:
     std::cout << inputFilePath << std::endl;
    show?
    The thing is, by the time that line gets executed, we know argv[1] is NOT "--help", and contains the input filepath, hence the name "inputFilePath" which is correct. That is why the length of the argument argv[1] I passed in was/is 34, but it outputs 32 when I output its size. I will go change the code though to output not only the size of the inputFilePath, but also the content as well. I just assumed before that the content was the same as the passed in filepath.
    Last edited by Programmer_P; 05-29-2010 at 09:54 PM.

  11. #26
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Lightbulb

    Hmm...
    The content of the input file path is the same as what I passed in except its missing the double quotes I surrounded it with. That explains why the size is 2 less than what it should be.
    Another odd thing is, I outputted the character at inputFilePathSize - 1 (which should be the last character of the string), and it reports that it is a '/' character instead of a 'p' character (last character of '.cpp') which is the last character of the inputFilePath string that gets outputted...
    Why the inconsistency, I have no idea.
    Last edited by Programmer_P; 05-29-2010 at 10:00 PM.

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Then it is the correct size because the quotes are removed.

  13. #28
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quotes around parms passed as arguments get stripped for you.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  14. #29
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Dino View Post
    Quotes around parms passed as arguments get stripped for you.
    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?
    Last edited by Programmer_P; 05-29-2010 at 10:06 PM.

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    So how do I check then to see if the user entered quotes around the filepath?

Page 2 of 5 FirstFirst 12345 LastLast
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, 03: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, 10:06 PM
  4. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 01:10 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21