[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

File CRC

Posted on 2004-11-05
4
Medium Priority
?
362 Views
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.
0
Comment
Question by:State
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 450 total points
ID: 12512807
You should check out the Boost library's CRC.  The documentation can be found at: http://www.boost.org/libs/crc/crc.html .  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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12513851
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;
                else
                    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;

    fclose(fp);

    return CRC;
}
0
 
LVL 55

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

829 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