Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fread problems

Status
Not open for further replies.

avivhal

Programmer
Nov 14, 2001
9
NL
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
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top