-
segFault
Code:
void hire(FILE **infile, char lineOfText[], struct employee **firstEmployee)
{
struct employee *newEmployee = NULL;
struct employee *previousEmployee = NULL;
struct employee *currentEmployee = NULL;
read(*infile, lineOfText);
while(lineOfText[ZEROI] != '*')
{
allocate(&newEmployee);
sscanf(lineOfText, "%s%s%d%s", newEmployee->lastName,
newEmployee->firstName,
&newEmployee->id,
newEmployee->class);
printf("%s\n", newEmployee->lastName);
if(newEmployee != NULL)
{
previousEmployee = NULL;
currentEmployee = *firstEmployee;
while((currentEmployee != NULL) && (strcmp(newEmployee->lastName, currentEmployee->lastName) > ZEROI))
{
previousEmployee = currentEmployee;
currentEmployee = currentEmployee->nextEmployee;
}
I"m getting a seg fault at the while loop, I have determined that this is due to the currentEmployee. setting currentEmployee = to *firstEmployee though I would think would solve this but I'm not getting anywhere.
-
Are you reading line outside the loop?
You should check that the read opepration is succeded in the while condition
sscanf(lineOfText, "%s%s%d%s", newEmployee->lastName
if(newEmployee != NULL)
you should check that newEmployee is NULL pointer, before you are trying to fill it