// BinCrcSum.cpp : 定义控制台应用程序的入口点。 //author:sunwei //2019/1/19 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <stdarg.h> #include <stdint.h> #include <time.h> #include <direct.h> #include "resource.h" #pragma warning(disable : 4996) #define PATH_B "./GSP_APP/Exe/GSP_APP.bin"//"D:/SVN_1015/GSP_TRUNK/GSP_APP/EWARM/GSP_APP/Exe/GSP_APP.bin" char* ReadFile (const char* szPath, size_t* pullsize) { assert (szPath != NULL); FILE* hFile = fopen (szPath, "rb+"); if(hFile == NULL) { printf("Path is error->%s ", szPath); getchar(); return NULL; } fseek (hFile, 0, SEEK_END); long lSize = ftell (hFile); rewind (hFile); size_t ullNum = lSize; char* pos = (char*) malloc (sizeof (char) * ullNum); assert (pos != NULL); fread (pos, sizeof (char), ullNum, hFile); fclose (hFile); *pullsize = ullNum; return pos; } void WriteFile (const char* szPath, char* pOutBuff, size_t size) { assert ( (szPath != NULL) && (pOutBuff != NULL) && (size > 0)); FILE* hFile = fopen (szPath, "a"); assert (hFile != NULL); fwrite (pOutBuff, sizeof (char), size, hFile); fclose (hFile); } void Little_32to8(uint32_t source, char *dest) { if (dest == NULL) return; dest[0] = (source >> 0) & 0xFF; dest[1] = (source >> 8) & 0xFF; dest[2] = (source >> 16) & 0xFF; dest[3] = (source >> 24) & 0xFF; } uint32_t U32SumCheck(uint8_t *dst, uint32_t size) { uint32_t sum = 0; while( size-- ) { sum += *dst++; } return sum; } int _tmain(int argc, _TCHAR* argv[]) { size_t FileSize = 0; uint32_t add; char a[4]; //打开bin文件 char* pBin = ReadFile(PATH_B, &FileSize); if((pBin == NULL) || (FileSize <= 0)) return 0; printf("GSP APP bin is %d ", FileSize); //算成累加和 add = U32SumCheck((uint8_t *)pBin, FileSize); pBin = NULL; Little_32to8(add, a); WriteFile(PATH_B, a, 4); pBin = ReadFile(PATH_B, &FileSize); assert((pBin != NULL) && (FileSize > 0)); printf("GSP APP bin add sum is %d ", FileSize); //追加到bin文件尾部 //getchar(); return 0; }