惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
T
The Blog of Author Tim Ferriss
H
Help Net Security
博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
博客园 - 聂微东
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
B
Blog
Engineering at Meta
Engineering at Meta
V
V2EX
Hugging Face - Blog
Hugging Face - Blog

博客园 - xiaobin80

Development Environment Preparation Clang IDE的选择在Windows Visual Studio 2017 and Fx EOL 跟我学python(5)- 匹配数字 跟我学python(4)- 正则 跟我学python(3)--- 带shell功能的hello world 跟我学python(2)- 第一个程序 跟我学python --- 搭建开发平台 Debian server 安装 debian 与 ubuntu 之 - sudo Try Value docker ce on Debian 使用hugo在gitee上写blog spring cloud gateway - RequestRateLimiter postman使用 改进的冒泡算法 Create User - mysql COM调用 – VB、PB J-Link clone问题 修复山寨版的J-Link
计算文件CRC32数值
xiaobin80 · 2025-11-04 · via 博客园 - xiaobin80

我们经常要计算文件的CRC32值。

经常用的工具为:HashCalc

hashCalc2026

fstream

使用方法:

  • 1.打开文件
 open(const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out)
  • 2.定位文件(读)
seekg(streamoff off, ios_base::seekdir way)
  • 3.文件大小
tellg()
  • 4.清错误状态标记
clear()
  • 5.读文件
read(char* s, streamsize n)
  • 6.关闭文件
close()

运行在VS Code:

//============================================================================
// Name        : calFileCRC32.cpp
// Author      : xiaobin
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>

using namespace std;

unsigned int crc32table[256];

void genCRC32table()
{
    int i;
    int j;
    unsigned int crc;
    for(i = 0; i < 256; i++) {
        crc = i;
        for(j = 0; j < 8; j++) {
            if((crc & 1) == 1)
                crc = (crc >> 1) ^ 0xEDB88320;
            else
                crc >>= 1;
        }
        crc32table[i] = crc;
    }
}

unsigned int calCRC32Num(char* fileName)
{
    genCRC32table();

    fstream fileStream;

    fileStream.open(fileName, ios::in | ios::binary); // Input and Binary

    fileStream.seekg(0L, ios::end);

    int fileSize = fileStream.tellg();

    char* buffer = new char[fileSize];
    int len = fileSize;

    fileStream.seekg(0, ios::beg);
    fileStream.clear();

    // =========================================================================
    unsigned int crc32 = 0xFFFFFFFF;

    while(!fileStream.eof()) // 是否文件末尾
    {
        if (!fileStream.fail()) { // 是否文件错
            fileStream.read(buffer, len);
            for(int i = 0; i < len; i++)
                crc32 = (crc32 >> 8) ^ crc32table[(crc32 ^ buffer[i]) & 0xFF];
        }
        fileStream.setstate(ios::eofbit); // 设置文件状态标记为eof
    }
   // =========================================================================

    fileStream.close();

    return crc32 ^ ~0U;
}

int main(int argc, char* argv[])
{
    printf("File Name\t\tCRC32\n");
    printf("%s\t", argv[1]);
    if (argc > 1) {
        // "d:\\D2172017.DAT"
        printf("%x", calCRC32Num(argv[1]));
    }
    return 0;
}

VS Code+LLVM

calFileCRC32.exe d:\\D2172017.DAT

command2026-02-15prompt

参考文章

  1. fstream - C++ Reference
  2. crc32:http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/crc32.c