Code:
//This function will read in the file students.in
//and store the information in struct class student[].
//It will then return the number of students.
int readfile(FILE *ifp, struct class student[])
{
int num, index, i, hours, grade, newgrade, counter=0;
char first[20];
char last[20];
char tempgrade[2];
char tempnewgrade[2];
//Read in number of records.
fscanf(ifp, "%d", &num);
//Read in each record one by one.
for (index=0; index<num; index++)
{
//Scan in the name from the file.
fscanf(ifp, "%s", last);
fscanf(ifp, "%s", first);
//Check for previous records for this student.
//Returns the index of where student is stored in struct.
i = search(student, num, last, first);
if (i < 0) //Enter in a new student.
{
counter++;
strcpy( student[index].last, last);
strcpy( student[index].first, first);
fscanf(ifp, "%s", student[index].action);
//If they are being removed, set credit hours to -1
if( strcmp(student[index].action, "REMOVE") == 0)
{
counter--;
student[index].hours = -1;
}
if( strcmp(student[index].action, "TOOK_CLASS") == 0 && student[index].hours >= 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
//Add in hours and grade points to running total.
student[index].hours += hours;
student[index].grade += (getgrade(tempgrade[0])*hours);
}
//If they are doing grade forgiveness, remove hours and grade.
if( strcmp(student[index].action, "GRADE_FORGIVE") == 0 && student[index].hours >= 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
//Add in hours and grade points to running total.
student[index].hours -= hours;
student[index].grade -= (getgrade(tempgrade[0])*hours);
}
//If they are changing their grade, scan in hours,
// old grade, and new grade.
if( strcmp(student[index].action, "CHANGE_GRADE")== 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
fscanf(ifp, "%s", tempnewgrade);
//Subtract the old points and add in the new ones.
student[index].grade -= (getgrade(tempgrade[0])*hours);
student[index].grade += (getgrade(tempnewgrade[0])*hours);
}
}
else //Otherwise, add information to student.
{
fscanf(ifp, "%s", student[index].action);
//If they are being removed, credit hours set to a negative number.
if( strcmp(student[index].action, "REMOVE") == 0)
student[i].hours = -1;
//If they are taking a class, add in the hours and grade.
if( strcmp(student[index].action, "TOOK_CLASS") == 0 && student[i].hours >= 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
//Add in hours and grade points to running total.
student[i].hours += hours;
student[i].grade += (getgrade(tempgrade[0])*hours);
}
//If they are doing grade forgiveness, remove hours and grade.
if( strcmp(student[index].action, "GRADE_FORGIVE") == 0 && student[i].hours >= 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
//Add in hours and grade points to running total.
student[i].hours -= hours;
student[i].grade -= (getgrade(tempgrade[0])*hours);
}
//If they are changing their grade, scan in hours,
// old grade, and new grade.
if( strcmp(student[index].action, "CHANGE_GRADE")== 0 && student[i].hours >= 0)
{
fscanf(ifp, "%d", &hours);
fscanf(ifp, "%s", tempgrade);
fscanf(ifp, "%s", tempnewgrade);
//Subtract out the points from the old grade and
//add in points for new one. Do nothing with the
//hours because we assume it was already added in
//previously with TOOK_CLASS.
student[i].grade -= (getgrade(tempgrade[0])*hours);
student[i].grade += (getgrade(tempnewgrade[0])*hours);
}
}
}
return counter;
}