File CRC

Posted on 2004-11-05
Last Modified: 2010-04-01
How would one go about returning the CRC of a file given the name stored in a char*, in non-MFC C++ or C. I've been trying different things but just can't get it to work.
Question by:State
    LVL 8

    Accepted Solution

    You should check out the Boost library's CRC.  The documentation can be found at: .  You just need to read the file into a buffer and call the process_bytes and checksum functions.  This header file should be able to be included in any relatively standard-conforming C++ compiler.
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Some C code:

    DWORD get_crc_32(char *filename)
        static unsigned long crc_table[256];
        static bool generated = false;
        unsigned long crc;
        FILE *fp;
        int c;
        if (!generated) {
            unsigned long crc, p;
            int    i, j;
            for (i = 0; i < 256; i++) {
                crc = i;
                for (j = 8; j > 0; j--) {
                    if (crc & 1)
                        crc = (crc >> 1) ^ 0xEDB88320L;
                        crc >>= 1;
                crc_table[i] = crc;
            generated = true;

        if ( !(fp=fopen(filename, "rb")))
            return NULL;                               <------------------------ When error occurs, return 0

        crc = 0xFFFFFFFF;
        while ((c = getc(fp)) != EOF)
            crc = (crc>>8) ^ crc_table[ (crc^c) & 0xFF ];
        crc ^= 0xFFFFFFFF;


        return CRC;
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Have provided a functions that does what author resquested, waiting for some feeback...

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now