5

I have read an xml file into a char [] and am trying to compare each element in that array with certain chars, such as "<" and ">". The char array "test" is just an array of one element and contains the character to be compared (i had to do it like this or the strcmp method would give me an error about converting char to cons char*). However, something is wrong and I can not figure it out. Here is what I am getting:
< is being compared to: < strcmp value: 44

Any idea what is happening?

char test[1];   
for (int i=0; i<amountRead; ++i)
{
    test[0] = str[i];
    if( strcmp(test, "<") == 0)
        cout<<"They are equal"<<endl;
    else
    {
        cout<<test[0]<< " is being compare to: "<<str[i]<<" strcmp value= "<<strcmp(test, "<") <<endl;
    }

}
hakre
  • 193,403
  • 52
  • 435
  • 836
Isawpalmetto
  • 785
  • 3
  • 12
  • 18
  • 1
    You should understand errors like `char` to `const char*` and fix it with your understanding, not hack around it. Also, showing us the output you're getting isn't an error to us, we have no idea what it's actually suppose to say. :) Lastly, this is C++! Use `std::string` and make your life easier. – GManNickG Feb 07 '10 at 06:13
  • 4
    you are comparing one element at a time, you do not need to compare strings. compare char instead, `'<' == str[i] ` – Anycorn Feb 07 '10 at 06:15
  • I would use the string class, but I am reading an xml file that could potentially have no whitespace (i.e. asdfsafasdfsaf So I figured the best way would be to do character by character. Anyway the "'<' == str[i]" ended up working (though I swore I already tried that). Thanks. – Isawpalmetto Feb 07 '10 at 06:29

3 Answers3

4

strcmp() expects both of its parameters to be null terminated strings, not simple characters. If you want to compare characters for equality, you don't need to call a function, just compare the characters:

if (test[0] == '<') ...
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
2

you need to 0 terminate your test string.

char test[2];   
for (int i=0; i<amountRead; ++i)
{
    test[0] = str[i];
    test[1] = '\0'; //you could do this before the loop instead.
    ...

But if you always intend to compare one character at a time, then the temp buffer isn't necessary at all. You could do this instead

for (int i=0; i<amountRead; ++i)
{
    if (str[i] == "<")
       cout<<"They are equal"<<endl;
    else
    {
        cout << str[i] << " is being compare to: <" << endl;
    }
}
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
  • 1
    Yuck, or not use `strcmp` at all. :P That'd be hacking the hack. – GManNickG Feb 07 '10 at 06:18
  • Agreed, `if (str[i] == '<'` makes more sense than the current code, but strcmp makes more sense if the plan is to compare larger sections later. – John Knoeller Feb 07 '10 at 06:24
  • Why copy when you can use strncmp? – R Samuel Klatchko Feb 07 '10 at 06:37
  • Yeah, using the == is working for me now. I just feel like there should be a way more efficient way to read and compare from a text file. In java, I could definitely do this better, but this is my first text parsing program in c++. – Isawpalmetto Feb 07 '10 at 06:38
  • @lsawpalmetto: Yes, there is a real flaw in the standard C string routines when it comes to parsing because they all expect to be passed null terminated inputs, but that's awkward when parsing a large string in pieces. have a look at strstr(), it's better than strcmp for what you are trying to do. – John Knoeller Feb 07 '10 at 06:43
1

strcmp wants both strings to be 0 terminated.

When you have non-0 terminated strings, use strncmp:

if( strncmp(test, "<", 1) == 0 )

It is up to you to make sure that both strings are at least N characters long (where N is the value of the 3rd parameter). strncmp is a good functions to have in your mental toolkit.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187