I wanted to get a checksum of a file.
when I wrote the following function with "#define READ_BUFFER_SIZE 1024"
It didn't get the right checksum for very big files(it did work for smaller files).
I changed it to "#define READ_BUFFER_SIZE 2048"
and it worked!it shouldnt really matter so I fail to understand the reason for it!
can anyone please explane the reason for it?
thank you.
Aviv.
BOOL FileCheckSum(CString FileName, unsigned long *Sum/*UINT32 * Sum*/)
{
#define READ_BUFFER_SIZE 2048//1024
FILE *fp;
char DataBuffer[READ_BUFFER_SIZE];
INT32 length;
fp=fopen(FileName,"r"
if(fp==NULL)
return FALSE;
*Sum=0;
while(!feof(fp))
{
/* Reset the data buffer before reading */
for(int i=0;i<READ_BUFFER_SIZE;i++)
DataBuffer [ i ] = 0;
length = fread(DataBuffer,1,READ_BUFFER_SIZE,fp);
while (length>0)
{
if(DataBuffer[length]=='\n')
*Sum += '\r'; //need to add CR to count for every LF since MC does it
*Sum += DataBuffer[--length];
}
}
/* Add 1 to the calculated CheckSum (agreed with the BMDS) */
*Sum += 1;
fclose(fp);
return TRUE;
#undef READ_BUFFER_SIZE
}
when I wrote the following function with "#define READ_BUFFER_SIZE 1024"
It didn't get the right checksum for very big files(it did work for smaller files).
I changed it to "#define READ_BUFFER_SIZE 2048"
and it worked!it shouldnt really matter so I fail to understand the reason for it!
can anyone please explane the reason for it?
thank you.
Aviv.
BOOL FileCheckSum(CString FileName, unsigned long *Sum/*UINT32 * Sum*/)
{
#define READ_BUFFER_SIZE 2048//1024
FILE *fp;
char DataBuffer[READ_BUFFER_SIZE];
INT32 length;
fp=fopen(FileName,"r"
if(fp==NULL)
return FALSE;
*Sum=0;
while(!feof(fp))
{
/* Reset the data buffer before reading */
for(int i=0;i<READ_BUFFER_SIZE;i++)
DataBuffer [ i ] = 0;
length = fread(DataBuffer,1,READ_BUFFER_SIZE,fp);
while (length>0)
{
if(DataBuffer[length]=='\n')
*Sum += '\r'; //need to add CR to count for every LF since MC does it
*Sum += DataBuffer[--length];
}
}
/* Add 1 to the calculated CheckSum (agreed with the BMDS) */
*Sum += 1;
fclose(fp);
return TRUE;
#undef READ_BUFFER_SIZE
}