Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2003-06-10 00:08:26


Hi Alex,

> I'd like to calculate a value for a cyclic redundancy check (crc) for a
> number of files.
>
> This isn't going to be transmitted anywhere, I simply want to compare the
> values to see whether the files are exactly the same. I know I can simply
> check byte for byte, but I've got a few thousand files and it's easier to
> calculate the crc for each and see whether any are the same first.

Here's the function that I use for the same purpose. Might not be optimal,
but works:

unsigned file_crc(const string& name)
{
#if defined(__GNUC__) && __GNUC__ < 3
    ifstream ifs(name.c_str(), ios::binary);
#else
    ifstream ifs(name.c_str(), ios_base::binary);
#endif
    if (!ifs)
        return 0;
    else {
        using namespace boost;

        crc_32_type crc32;
        int c;
        while( (c = ifs.rdbuf()->sbumpc()) != -1)
             crc32.process_byte(char(c));
        return crc32.checksum();
    }
}

I believe some example of this kind should be included... Daryle, what do
you think?

HTH,
Volodya


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net