in the program i built from a "students.txt" file a binary tree. and from each students i make a new linked list the the grades that i took from "grades.txt" file.
the code i put here is the code for the 2 functions that build the grades list from the "grades.txt" file, the programs runs but break in the while loop. i dont know why?? help please, thanks!! if you want to see the whole code you can enter: C | // -----------------------------------------------
the students.txt file is build from:
id number - 9 chars
first name - 26 chars
last name - 26 chars
space between each field.
the grades.txt file is build from:
id number - 9 chars
grade - int
exercise - int
space between each field.
2 functions code, the second calls the first, the break happen in the second in the while loop:
Code:
// ----------------------------------------------------
// Synopsis: Recursively insert student grades to tree.
//
// Arguments:
// - Grades file name
// - pointer for tree root
//
// Return-Value: FALSE on failure. TRUE otherwise
// ----------------------------------------------------
int InsertGradesTreeNode(StudentNode * proot, GradeNode * newGrade, char id[ID_SIZE])
{
StudentNode * root;
GradeNode *nextNode = proot->student.gradeList;
int cmpResult;
if (proot == NULL)
return FALSE;
root = proot; // Used just of cosmetics
cmpResult = CompareId(id, root->student.Id);
if (cmpResult == 0)
{
for (; nextNode->next != NULL; nextNode = nextNode->next);
nextNode->next = newGrade;
return TRUE;
}
// Advance right of left, based on compare
if (cmpResult > 0)
{
return InsertGradesTreeNode(root->right, nextNode, id);
}
if (cmpResult < 0)
{
return InsertGradesTreeNode(root->left, nextNode, id);
}
return TRUE;
}
// ----------------------------------------------------
// Synopsis: Load student grade list to appropriate
// student node.
//
// Arguments:
// - grades file name
// - pointer for tree root
//
// Return-Value: FALSE on failure. TRUE otherwise
// ----------------------------------------------------
int LoadGradesFromFile(const char * filename, StudentNode * proot)
{
GradeNode gradeN;
FILE * gradesFile;
char id[ID_SIZE];
// Zero gradeN in advance
gradeN.next = NULL;
gradeN.grade.grade = 0;
gradeN.grade.exercise = 0;
// Open grades file
gradesFile = fopen(filename, "r");
if (gradesFile == NULL)
{
fprintf(stderr, "fopen(%s) failed\n", filename);
return FALSE;
}
// Add each student grades in file into tree
while (EOF != fscanf(gradesFile, "%9s %d %d", id, gradeN.grade.grade, gradeN.grade.exercise))
{
if (!InsertGradesTreeNode(proot, &gradeN, id))
{
fprintf(stderr, "CreateGradeReport failed!\n");
fclose(gradesFile);
return FALSE;
}
}
// close and exit
fclose(gradesFile);
return TRUE;
}