Created
March 17, 2015 08:16
-
-
Save jsfaint/2acdf3dd19628ab18ee4 to your computer and use it in GitHub Desktop.
Calculate checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char* argv[]) | |
{ | |
char *szFilename = 0; | |
FILE *fp; | |
unsigned long data; | |
unsigned long checksum = 0; | |
int size = 0; | |
if (argc == 1) | |
printf("need input file name\n"); | |
szFilename = argv[1]; | |
fp = fopen(szFilename, "rb"); | |
while(fread(&data, 4, 1, fp)) | |
{ | |
size += 4; | |
checksum ^= data; | |
} | |
fclose(fp); | |
printf("size=%d, checksum=%08X\n", size, checksum); | |
fp = fopen(szFilename, "ab"); | |
fwrite(&checksum, 4, 1, fp); | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment