Comparing strings of more than 50 characters

howdy,
I'm trying to compare 4 strings with a simple "if" like this:

std::vector <string> _fileNameBuffer;
std::vector <string> _filePathBuffer;
string file_to_find;
string path_to_find;

if(_fileNameBuffer.at(i).compare(file_to_find) == 0 &&_filePathBuffer.at(i) == path_to_find ){
cout << "FOUND!" << endl;
return 1;
}

_fileNameBuffer and _filePathBuffer are string-vectors declared as you can see above, and the other 2 strings are just simple strings.

The problem is that even if the strings are the same it's not working, so it's not going through the if statement, I think is the length of the strings file_to_find and _fileNameBuffer.at(i), because they can have more than 50 chars, and maybe the function "compare" has some kind of limit.

With this information do you know what could be the problem.

Are you sure the strings are exactly the same? Same length, same whitespace, same letter case?
DIY?

make a for loop and compare one char at a time.
when they are not equal, print the individual characters in hex and the index.

should show you what is different.

Ive compared GB sized buffers representing large xml files. The tools work fine. Its not related to the length. Its really easy to have a small unseen difference though, which is why we let the computer do the dirty work of figuring out where that is.


Last edited on
std::mismatch() Returns the first mismatching pair of elements from two ranges
http://en.cppreference.com/w/cpp/algorithm/mismatch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <algorithm>

// silently return true if a == b. if not, print out
// information about the mismatch and return false
bool debug_expected_equal( const std::string& a, const std::string& b )
{
    if( a.size() != b.size() )
    {
        // mismatched string lengths: print out the string sizes
        std::cout << "size mismatch: " << a.size() << " and " << b.size() << '\n' ;
        return false ;
    }

    // http://en.cppreference.com/w/cpp/algorithm/mismatch
    const auto pair = std::mismatch( a.begin(), a.end(), b.begin() ) ;
    if( pair.first == a.end() ) return true ; // a and b compare equal

    // mismatched strings: print out mismatched position and characters
    std::cout << "mismatch at pos " << pair.first - a.begin()
              << " chars: '"  << *pair.first << "' (" << int(*pair.first) << ')'
              << " and '" << *pair.second << "' (" << int(*pair.second) << ")\n" ;
    return false ;
}

int main()
{
    const std::string a = "The problem is that even if the strings are the same it's not "
                          "working, so it's not going through the if statement, I think is "
                          "the length of the strings file_to_find and _fileNameBuffer.at(i), "
                          "because they can have more than 50 chars, and maybe the function "
                          "\"compare\" has some kind of limit." ;

    std::string b = a ;

    std::cout << std::boolalpha << debug_expected_equal(a,b) << "\n\n" ; // true

    b += '!' ;
    std::cout << debug_expected_equal(a,b) << "\n\n" ; // size mismatch: 290 and 291
                                                       // false
    b.pop_back() ;
    b[217] = 'Z' ;
    std::cout << debug_expected_equal(a,b) << '\n' ; // mismatch at pos 217 chars: 'e' (101) and 'Z' (90)
                                                     // false    
}

http://coliru.stacked-crooked.com/a/6027d52a3c60ea04
Topic archived. No new replies allowed.