Trying to read file... with checksums
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));

