Trying to read file... with checksums

My file is a binary file with the following format:
1 byte = P (for port)
1 byte = port number
1 byte = data length (number of bytes in payload)
n bytes = payload (n is the data length)
2 bytes = checksum

Unfortunately, the current code (marked in the code below) doesn't produce the correct payloadChecksums. I have included some code that DOES produce the correct checksums.

Does somebody have a suggestion as to how to make it so that I can calculate the correct checksums and write payload to another file?


unsigned long int counter=0,checksumsRemoved=0;
char payload [256];
unsigned short int i,portNumber,dataLength,payloadChecksum,checksum;
do
{
ch = getc(inFile);
if(ch == 'P')
{
payloadChecksum=0; // reset the checksum for every record
// We are going to create a new file for every port so we need a list of
// all of the ports used.
portNumber = getc(inFile);
cout << portNumber << " ";
ports.push_back(portNumber);
dataLength = getc(inFile);
cout << dataLength << " \n";
// NEED THIS CODE TO WORK SO I CAN WRITE payload TO A FILE
// the following seven lines do NOT provide the correct payloadChecksums
unsigned long int temp;
fgets(payload, dataLength-3, inFile);
for(i=1;i<dataLength-2;i++)
{ temp = payload[i-1];
cout << temp << "+";
payloadChecksum += temp;
}
// DEBUG CODE
// the following code produces the correct payloadChecksums
//for(int i=1;i<dataLength-2;i++)
//{
// temp = getc(inFile);
// cout << temp << "+";
// payloadChecksum += temp;
//}
// The payload checksum is a sum of all bytes in the payload
// The checksum that comes from the PedStar is two separate bytes. We
// must combine them to get the number to compare our computed value to.
for(i=1;i<=2;i++)
if(i==1)
checksum = 256 * getc(inFile);
else
checksum += getc(inFile);
cout << "=" << payloadChecksum << "=" << checksum << endl;
if(checksum != payloadChecksum)
{ badRecords.push_back(counter);
checksumsRemoved++;
}

counter++;
}
} while(!feof(inFile));
[2576 byte] By [sidkdbl07] at [2007-11-20 11:09:58]
# 1 Re: Trying to read file... with checksums
My file is a binary file with the following format
You said you're reading a binary file so using fgets() is not the right function to read from the file. Use fread() instead.
BTW did you open inFile in binary mode ?
Kurt
ZuK at 2007-11-9 1:25:13 >
# 2 Re: Trying to read file... with checksums
BTW, what is the reason for having this code so complex?
for(i=1;i<=2;i++)
if(i==1)
checksum = 256 * getc(inFile);
else
checksum += getc(inFile);
Isn't it the same as:
checksum = 256 * getc(inFile);
checksum += getc(inFile);?
VladimirF at 2007-11-9 1:26:06 >
# 3 Re: Trying to read file... with checksums
OK, I tried fread but the problem persists...

I've included a source file and a sample data file.
sidkdbl07 at 2007-11-9 1:27:15 >
# 4 Re: Trying to read file... with checksums
just a signed/unsigned problem
for(i=1;i<dataLength-2;i++)
{ temp = (unsigned char)payload[i-1];
cout << temp << "+";
payloadChecksum += temp;
}
I wouldn't mix binary and text input. Use fread to read single chars as well.
Kurt
ZuK at 2007-11-9 1:28:12 >