This program is to read record from a file and then validate
the 5th element str[5].
From the valid check digits out of 45 recs only 5 is reported
valid. I dont know what is wrong with this code
Example of rec on file is c12543256 my name where i leave......\n
please help, am i reading the record wrongly?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[107];
char temp[5], chk_digit, *c_ptr;
int weights[] = {5, 4, 3, 2}, *w_ptr;
int total=0, value, rem, check_num;
while(fgets(str,sizeof(str), fp) != NULL)
{
w_ptr = weights;
strncpy(temp,&str[1],4);
for(c_ptr=temp; *c_ptr; c_ptr++)
{
value = *c_ptr - '0';
total += value * *w_ptr++;
}
rem = total % MODULUS;
check_num = MODULUS - rem;
if( check_num < 10 )
chk_digit = check_num + '0';
else
if( check_num == 10 )
chk_digit = '0';
else
chk_digit = 'X';
if(chk_digit != str[5])
{
fprintf(prn_ptr,"\nError in code");
}
else
fputs(str,fv);
}
fprintf(prn_ptr,"\f");
}
thanks