c++stringcomparisonbinaryfiles

What's the fastest way to tell whether two strings or binary files are different?


I'm writing a unit test and need to compare a result file to a golden file. What's the easiest way to do so?

So far I have (for Linux environment):

int result = system("diff file1 file2");

They are different if result != 0.


Solution

  • If you want a pure c++ solution, I would do something like this

    #include <algorithm>
    #include <iterator>
    #include <string>
    #include <fstream>
    
    template<typename InputIterator1, typename InputIterator2>
    bool
    range_equal(InputIterator1 first1, InputIterator1 last1,
            InputIterator2 first2, InputIterator2 last2)
    {
        while(first1 != last1 && first2 != last2)
        {
            if(*first1 != *first2) return false;
            ++first1;
            ++first2;
        }
        return (first1 == last1) && (first2 == last2);
    }
    
    bool compare_files(const std::string& filename1, const std::string& filename2)
    {
        std::ifstream file1(filename1);
        std::ifstream file2(filename2);
    
        std::istreambuf_iterator<char> begin1(file1);
        std::istreambuf_iterator<char> begin2(file2);
    
        std::istreambuf_iterator<char> end;
    
        return range_equal(begin1, end, begin2, end);
    }
    

    It avoids reading the entire file into memory, and stops as soon as the files are different (or at end of file). The range_equal because std::equal doesn't take a pair of iterators for the second range, and isn't safe if the second range is shorter.