Search code examples
c++c++20checksumcrc

Calculate CRC for File(content)


I tried to create a program to create a crc sum for a file, based on its content. My intention is to compare this with a older value to see if content changed. I work on Linux (Debian). I tried this code below and it seems to work:

FileInfo fi;
fi.strPath=strPath_;
std::ifstream input(fi.strPath, 
std::ios::binary);
std::vector<char> bytes(
   (std::istreambuf_iterator<char>(input)),
   (std::istreambuf_iterator<char>()));
input.close();
unsigned long long ullNumber = 0;
for( auto &bytes:bytes )
    {
        ullNumber += (unsigned long long)bytes;
    }
cout << "Number: "<<ullNumber<<"\n";
fi.ulHash=ullNumber;
mvctFileInfoNew.push_back(fi);

My Question is: Is this way "ok" to use or bad (unfortunatly Iam not so experienced) and I would like to get some opinion from someone more experienced to imrove my skills.


Solution

  • The thing is, the value does not change.

    It's not supposed to. The filesystem::hash_value is a hash of the path to the file, not the contents of the file. If you want to compute a CRC of the contents of a file, you're going to have to read those contents and apply a CRC algorithm to them.