Re: Fast way to compare two files using STL?

Discussion in 'C++' started by John Villalovos, Aug 18, 2003.

  1. In article <>,
    foo <> wrote:
    > (John Villalovos) wrote in message
    >news:<bhjsa8$acm$>...
    >> I was wondering if people had any ideas on a fast way to compare two files
    >> (on Linux) using the STL.
    >>
    >> Currently I am trying something like this (error checking removed)
    >>
    >>
    >> bool areFileContentsEqual( const string & filename1, const string & filename2 )
    >> {
    >> bool result = false;
    >>
    >> // Open our two files
    >> ifstream file1( filename1.c_str(), ios::binary );
    >> ifstream file2( filename2.c_str(), ios::binary );
    >> // Handy typedef
    >> typedef istream_iterator<char> char_input;
    >> // Use the STL algorithm EQUAL to determine if the contents of the two
    >> // files are equal.
    >> result = equal( char_input( file1 ), char_input(),
    >> char_input( file2 ) );
    >> return result;
    >> }
    >>
    >> The problem is that performance is pretty bad. I was hoping to figure out how
    >> to get performance to be better.
    >>
    >> Any ideas would be greatly appreciated.
    >>

    >This function will give a false positive if file2 is smaller then
    >file1, and the contents of file2 matches the front end of file1.


    Thanks. Yep I already knew this. It is commented in the code about this
    precondition.

    John
    John Villalovos, Aug 18, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    1
    Views:
    856
    Pete Becker
    Aug 16, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    430
    Kevin Goodsell
    Aug 16, 2003
  3. Thomas Matthews
    Replies:
    1
    Views:
    886
    John Villalovos
    Aug 18, 2003
  4. GenxLogic
    Replies:
    3
    Views:
    1,262
    andrewmcdonagh
    Dec 6, 2006
  5. Replies:
    5
    Views:
    500
    Markus Schoder
    Apr 16, 2006
Loading...

Share This Page