Professional Documents
Culture Documents
First things first. Begin withe spliting up the code into different subsets.
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
/*
The code is a student management system written in the C programming
language. This program allows users to create, display, update, delete,
and search for student records.
It uses a struct to represent a student, and a binary file to store the
student records. The program consists of a main function that displays
a menu and calls the appropriate functions depending on the user's
input.
*/
switch (option)
{
case '1':
createAccount();
break;
case '2':
displayInfo();
break;
case '3':
updateInfo();
break;
case '4':
deleteInfo();
break;
case '5':
searchInfo();
break;
case '0':
printf("\n\t\t\t====== Thank You ======");
break;
default:
printf("\n\t\t\tInvalid Option, Please Enter Right Option !
\n");
}
}
return 0;
}
VOID CREATE STUDENT
void createAccount()
{
FILE *fileOne = fopen("studentInfo.txt", "ab+");
if (fileOne == NULL)
{
printf("\n\t\t\tError !\n");
}
Student stundentInformation;
system("cls");
fwrite(&stundentInformation, sizeof(stundentInformation), 1,
fileOne);
fclose(fileOne);
}
VOID DISPLAY
void displayInfo()
{
FILE *fileOne = fopen("studentInfo.txt", "rb");
if (fileOne == NULL)
{
printf("\n\t\t\tError !\n");
}
Student stundentInformation;
system("cls");
fclose(fileOne);
}
VOID UPDATE
void updateInfo()
{
FILE *fileOne = fopen("studentInfo.txt", "rb");
FILE *temp = fopen("temp.txt", "wb");
system("cls");
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else if (choice == 2)
{
printf("\n\t\t\tEnter Student's Grade to Update: ");
getchar();
gets(tempInformation.studentGrade);
strcpy(studentInformation.studentGrade, tempInformation.
studentGrade);
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else if (choice == 3)
{
printf("\n\t\t\tEnter Mother's name to Update: ");
getchar();
gets(tempInformation.mothersName);
strcpy(studentInformation.mothersName, tempInformation.
mothersName);
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else if (choice == 4)
{
printf("\n\t\t\tEnter Father's name to Update: ");
getchar();
gets(tempInformation.fathersName);
strcpy(studentInformation.fathersName, tempInformation.
fathersName);
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else if (choice == 5)
{
printf("\n\t\t\tEnter Student's Contact No. to Update:
");
getchar();
gets(tempInformation.studentContactNum);
strcpy(studentInformation.studentContactNum,
tempInformation.studentContactNum);
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else if (choice == 6)
{
printf("\n\t\t\tEnter School fee amount to Update: ");
scanf("%f", &tempInformation.schoolFeeAmt);
studentInformation.schoolFeeAmt = tempInformation.
schoolFeeAmt;
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
printf("\n\n\t\t\tUpdated successfully!\n\n");
}
else
{
printf("\n\t\t\tInvalid Option.");
fwrite(&studentInformation, sizeof(studentInformation),
1, temp);
}
}
else
{
fwrite(&studentInformation, sizeof(studentInformation), 1,
temp);
}
}
fclose(fileOne);
fclose(temp);
remove("studentInfo.txt");
rename("temp.txt", "studentInfo.txt");
if (flag == 0)
{
printf("\n\t\t\tStudent Id is not found");
}
VOIDE DELETE
void deleteInfo()
{
FILE *fileOne = fopen("studentInfo.txt", "rb");
FILE *temp = fopen("temp.txt", "wb");
system("cls");
fclose(fileOne);
fclose(temp);
remove("studentInfo.txt");
rename("temp.txt", "studentInfo.txt");
if (flag == 0)
{
printf("\n\t\t\tStudent Id is not found");
}
VOID SEARCH
void searchInfo()
{
FILE *fileOne = fopen("studentInfo.txt", "rb");
Student studentInformation;
if (fileOne == NULL)
{
printf("\n\t\t\tError !\n");
}
system("cls");
if (choice == 1)
{
system("cls");
printf("\t\t\t\t====== Search Student Information ======\n");
printf("\n\n\t\t\tEnter Student ID : ");
getchar();
gets(studentID);
while (fread(&studentInformation, sizeof(studentInformation),
1, fileOne) == 1)
{
if (strcmp(studentInformation.studentId, studentID) == 0)
{
flag++;
printf("\n\t\t\tStudent Name : %s\n\t\t\tStudent ID : %
s\n\t\t\tStudent Grade : %s\n\t\t\tMother's name : %s\n\t\t\tStudent
Contact No. : %s\n\t\t\tSchool fee amount : %.2f\n", studentInformation.
studentName, studentInformation.studentId, studentInformation.
studentGrade, studentInformation.mothersName, studentInformation.
fathersName, studentInformation.studentContactNum, studentInformation.
schoolFeeAmt);
}
}
if (flag == 0)
{
printf("\n\t\t\tStudent Id is not found");
}
}
else if (choice == 2)
{
system("cls");
printf("\t\t\t\t====== Search Student Information ======\n");
printf("\n\n\t\t\tEnter Student Grade. : ");
getchar();
gets(studentGrade);
printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-18s%-s\n", "Name",
"ID", "Grade", "Mother's name", "Father's name", "Contact", "School Fee
anount");
printf
("\t\t------------------------------------------------------------------
----------------------");
while (fread(&studentInformation, sizeof(studentInformation),
1, fileOne) == 1)
{
if (stricmp(studentInformation.studentGrade, studentGrade)
== 0)
{
flag++;
printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-18s%-.2f",
studentInformation.studentName, studentInformation.studentId,
studentInformation.studentGrade, studentInformation.mothersName,
studentInformation.fathersName, studentInformation.studentContactNum,
studentInformation.schoolFeeAmt);
}
}
if (flag == 0)
{
printf("\n\t\t\tStudent Id is not found");
}
}
else
{
printf("\n\t\t\tInvalid Option");
}
fclose(fileOne);
1. Updating the school fee section for it to actually calculate the school fee depending on the grade of the student.
2. Adding an age section where depending on the grade of the student there age will automatically be added as well. (if the age is wrong it
can be updated using the updateinfo option).
3. Simplify the code more so it fits in a more compact and easier to read format.