void dl8()
{
struct student studBuffer[5];
char studName[15];
int foundFlag, studentIndex, i;
f = fopen("Record.txt","r");
// If f is not NULL then load all the contents to buffer
if (f != NULL)
{
printf("Opening Student File Successful...\n");
// Fill our buffer of zeroes to clear out leftover garbage data.
memset(studBuffer,0,sizeof(studBuffer) * 5);
for(i=0;i<5;i++)
{
fscanf(f,"%s%s%s%d%lf%lf%lf%lf",
studBuffer[i].id,
studBuffer[i].name,
studBuffer[i].course,
&studBuffer[i].year,
&studBuffer[i].grade[0],
&studBuffer[i].grade[1],
&studBuffer[i].grade[2],
&studBuffer[i].fgrade);
}
// Now that we all the necessary data in the buffer
// we can close the file for now.
fclose(f);
}
else
{
printf("Opening Student File Failed...");
// Exit out of the function
return;
}
// Time to find out which student record to delete.
// But first we need to ask for the name of the student
printf("Enter the name of the student to remove out of the record: \n");
scanf("%s",studName);
// Now we have a name. Time to find out if that name
// is in our student records.
// Initialize our name is found flag.
// Set to 0 to signify as false.
foundFlag = 0;
// Loop counter
i = 0;
while (foundFlag != 1 && i < 5)
{
// If function strcmp returns 0 then we found a match.
if (strcmp(studName,studBuffer[i].name) == 0)
{
// Get the array index of the matching student name.
studentIndex = i;
foundFlag = 1;
}
i++;
}
// If no match is found. Return to the caller of the this function
// or exit out of this funtion.
if (foundFlag != 1)
{
printf("No found match on students name. \n");
// Exit out of function.
return;
}
// If we reached here it means we have found a match.
// Now time to write the changes in the file.
// Open the file in write mode.
f = fopen("Record.txt","w");
// If f is not NULL then write all the contents the buffer
// into the file except for the student to be removed.
if (f != NULL)
{
for(i=0;i<5;i++)
{
// Write into the file except for the array index
// of student to removed.
if (i != studentIndex)
{
fprintf(f,"%s\n%s\n%s\n%d\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n",
studBuffer[i].id,
studBuffer[i].name,
studBuffer[i].course,
studBuffer[i].year,
studBuffer[i].grade[0],
studBuffer[i].grade[1],
studBuffer[i].grade[2],
studBuffer[i].fgrade);
}
}
fclose(f);
printf("Student record deletion is successful.\n");
}
else
{
printf("Student record deletion is failed.\n");
}
}