How to write CRC/checksum code?

This is a discussion on How to write CRC/checksum code? within the C++ Programming forums, part of the General Programming Boards category; I'm coding an application, and I'm looking for an algorithm that can tell me if a file has changed, without ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    How to write CRC/checksum code?

    I'm coding an application, and I'm looking for an algorithm that can tell me if a file has changed, without only using the file's timestamp. I was thinking of using something like a checksum.

    This would be used for files < 20 MB in size, and on these files, it should be able to operate in a few seconds, preferably under 1s for files under 1 MB.

    My idea was to store the last modified date, the checksum, and the file length for each file as it was discovered, and later, to use these as a test to determine if the file was modified or replaced; if all 3 match, the file is considered unchanged; if not, the file has been altered.

    I just need a fast algorithm to generate some kind of checksum. Any resources?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Don't know how fast this is, but some files I have worked with of similar size used this algorithm (pseudocode)-

    checksum = 0
    previousByte = 0
    for (i = 0 to filesize - 1)

    checksum = checksum + fileByte[i] + (i / previousByte + 1)

    previousByte = fileByte[i]

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Hmm, I've been looking, but one issue is that most of them, including the only "official" CRC code I've found, end up with a word value (16 bits) or smaller.

    I'd prefer something that would come up with a longer CRC/checksum val, like 32 or 64 bits.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Doing a quick search turns up -

    http://www.createwindow.com/programming/crc32

    I don't know whether this meets your speed requirements, and if you've been looking around you may have already found this out for yourself.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Thanks, these should work out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  2. how to write code about delete process?
    By cc246 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2003, 08:23 PM
  3. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21