Home    | Products    | Downloads    | Purchase    | Support   

 Products

 Paint Express

 HashCalc

 FSUM

 QuickHash Library

    Download

    Purchase

    Samples

    License Agreement

    Related Links

    F.A.Q.

    Overview

 FastCRC Library

 Company

 About Us

 Contact Us

 Miscellaneous

 Affiliate Program

 Site Map

SlavaSoft QuickHash Library Samples
Sample #4 (C++ Interface)  

All

Previous Next


The following sample demonstrates how to use the CCRC32 class to calculate the CRC32 checksum for a file.

#include <fstream>
#include <iostream>
#include <QuickHash.h>

using namespace std;
using namespace QuickHash;

bool CalculateFileChecksum( const char* filename, char* checksumhex )
{
    fstream file( filename, ios::in | ios::binary );
   
    if( !file )
    {
        cout << "\nCould not open file: " << filename;
        return false;
    }
   
    const unsigned int BUFF_SIZE = 1024;
    unsigned char buff[ BUFF_SIZE ];
   
    //Instantiate a CCRC32 object
   
    CCRC32 hash;
   
    //Calculate the checksum by calling Update for each block of the file
   
    while( !file.eof() )
    {
        file.read( ( char* )buff, BUFF_SIZE );
        hash.Update( buff, file.gcount() );
    }
   
    //Do final changes and get the checksum in hex format
   
    hash.FinalHex( checksumhex ); // FinalHex reinitializes the hash object for the next use
   
    file.close();
   
    return true;
}

int main()
{
    char buff[ 10 ];
   
    char checksumhex[ CCRC32::HEXDIGESTSIZE ]; //0 terminated
   
    do
    {
        //Get the file name from the user
       
cout << "\nEnter a file name:\n";
       
        char filename[ 256 ];
        cin.getline( filename, 256 );
       
        //Calculate the checksum
       
        if( CalculateFileChecksum( filename, checksumhex ) )
            cout << "\nChecksum: " << checksumhex;
       
        //Continue?
       
        cout << "\nContinue (Y/N)?";
        cin.getline( buff, 10 );
       
    } while ( *buff == 'y' || *buff == 'Y' );
   
    return 0;
}
 
  Copyright SlavaSoft Inc. All rights reserved.