Hi All
I am loading a linked list from file pushing to the list from a reference pointer
However when i load the file for 2nd time (for debugging purpose to see if it rejects duplicates) one of my data got corrupted
The first time i load the file it loads perfectly fine
The 2nd time i load the employee code became -842150451, and the entire validation failed can anyone guide me on how to solve this problem?
Code:
int loadEntryForText(char *str , struct EmployeeData ** emp)
{
int n, exist, code;
int count = 0;
char field[30];
struct EmployeeData* currentEmp = *emp;
struct EmployeeData* newEmp = NULL;
struct Pay* newData = NULL;
struct Date* dob = NULL;
newEmp = malloc(sizeof(struct EmployeeData));
newData = malloc(sizeof(struct Pay));
dob = malloc(sizeof(struct Date));
//Start string parsing
while (sscanf(str, "%29[^|]%n", field, &n) == 1 ){
//printf("field = \"%s\"\n", field);
switch (count){
case 0:
code = atoi(field);
for (currentEmp=*emp; currentEmp!=NULL; currentEmp=currentEmp->nextElement){
if (code == currentEmp->code){ //Check if employee code exists
printf("\n\tThe employee code: %d you have entered already exist, \n\tand thus this entry will not be added.\n\n\t", code);
exist = TRUE;
pause();
}
else{
exist = FALSE;
newEmp->code = code;
}
}
//printf("%d\n", newEmp->code);
break;
case 1:
newEmp->name = (char*)malloc((strlen(field)+1)*sizeof(char));
strcpy(newEmp->name, field);
//printf("%s\n", newEmp->name);
break;
case 2:
newEmp->age = atoi(field);
//printf("%d\n", newEmp->age);
break;
case 3:
dob->day = atoi(field);
//printf("%d\n", dob->day);
break;
case 4:
dob->month = atoi(field);
//printf("%d\n", dob->month);
break;
case 5:
dob->year = atoi(field);
//printf("%d\n", dob->year);
break;
case 6:
newEmp->jobCat = field[0];
//printf("%c\n", newEmp->jobCat);
break;
case 7:
newEmp->basicPay = atof(field);
//printf("%lf\n", newEmp->basicPay);
break;
case 8:
newEmp->hours = atoi(field);
//printf("%d\n", newEmp->hours);
break;
case 9:
newData->otHours = atoi(field);
//printf("%d\n", newData->otHours);
break;
case 10:
newData->otPay = atof(field);
//printf("%lf\n", newData->otPay);
break;
case 11:
newData->weeklyPay = atof(field);
//printf("%lf\n", newData->weeklyPay);
break;
}
count++;
str += n; /* advance the pointer by the number of characters read */
if ( *str != '|' )
{
newEmp->dataElement = newData;
newEmp->dob = dob;
newEmp->nextElement = *emp;
*emp = newEmp;
break; // didn't find an expected delimiter
}
++str; /* skip the delimiter */
}
return 1;
}