#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.
*/
// the student typedef struct is created in order to input student data.
// typedef also allows us to use "Student" as a replacement for struct.
typedef struct Student
{
    char studentName[50];
    char studentId[15];
    char studentGrade[10];
    char fathersName[100];
    char mothersName[100];
    char studentContactNum[15];
    float schoolFeeAmt;
} Student;
// functions created to add, display, update, delete and search for students.
void createAccount();
void displayInfo();
void updateInfo();
void deleteInfo();
void searchInfo();
int main()
{
    // option used in the switch case and while loop operations to create the menu
of the program
    char option;
    while (option != '0')
    {
        system("cls");
        printf("\t\t======   Student Management Database System ======\n");
        printf("\n\t\t\t1.   Create Student Account");
        printf("\n\t\t\t2.   Display All Students Information");
        printf("\n\t\t\t3.   Update Student Information");
        printf("\n\t\t\t4.   Delete Student Information");
        printf("\n\t\t\t5.   Search Student Information");
        printf("\n\t\t\t0.   Exit");
        printf("\n\n\n\t\t\tEnter Your Option: ");
        scanf(" %c", &option);
        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 createAccount()
{
    FILE *fileOne = fopen("studentInfo.txt", "ab+");
    if (fileOne == NULL)
    {
        printf("\n\t\t\tError !\n");
    }
    Student stundentInformation;
    system("cls");
    printf("\t\t\t====== Create Student Account ======\n");
    printf("\n\t\t\tEnter Student's Name : ");
    getchar();
    gets(stundentInformation.studentName);
    printf("\t\t\tEnter Student's ID : ");
    gets(stundentInformation.studentId);
    printf("\t\t\tEnter Student's Grade : ");
    gets(stundentInformation.studentGrade);
    printf("\t\t\tEnter student's Mother's name : ");
    gets(stundentInformation.mothersName);
    printf("\t\t\tEnter student's Father's name : ");
    gets(stundentInformation.fathersName);
    printf("\t\t\tEnter Student's Contact Number : ");
    gets(stundentInformation.studentContactNum);
    printf("\t\t\tEnter School fee amount ($4000 extra is your a transfer
student) : ");
    scanf("%f", &stundentInformation.schoolFeeAmt);
    fwrite(&stundentInformation, sizeof(stundentInformation), 1, fileOne);
    printf("\n\n\t\t\tInformations have been stored sucessfully\n");
    printf("\n\n\t\t\tEnter any keys to continue.......");
    getch();
     fclose(fileOne);
}
void displayInfo()
{
    FILE *fileOne = fopen("studentInfo.txt", "rb");
     if (fileOne == NULL)
     {
         printf("\n\t\t\tError !\n");
     }
     Student stundentInformation;
     system("cls");
     printf("\t\t\t\t====== All Students Information ======\n");
    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");
    printf("\t\
t----------------------------------------------------------------------------------
------");
     while (fread(&stundentInformation, sizeof(stundentInformation), 1, fileOne) ==
1)
     {
        printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-18s%-.2f",
stundentInformation.studentName, stundentInformation.studentId,
stundentInformation.studentGrade, stundentInformation.mothersName,
stundentInformation.fathersName ,stundentInformation.studentContactNum,
stundentInformation.schoolFeeAmt);
    }
     printf("\n\n\t\tEnter any keys to continue.......");
     getch();
     fclose(fileOne);
}
void updateInfo()
{
    FILE *fileOne = fopen("studentInfo.txt", "rb");
    FILE *temp = fopen("temp.txt", "wb");
     Student studentInformation, tempInformation;
     int choice, flag = 0;
     if (fileOne == NULL || temp == NULL)
     {
         printf("\n\t\t\tError !\n");
     }
     system("cls");
     printf("\t\t\t\t====== Update Students Information ======\n");
    printf("\n\t\t\tEnter Student's ID : ");
    getchar();
    gets(tempInformation.studentId);
    while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
    {
        if (strcmp(studentInformation.studentId, tempInformation.studentId) == 0)
        {
            flag++;
            printf("\n\t\t\tChoose your option :\n\t\t\t1.Update Student Name\n\t\
t\t2.Update Student Grade.\n\t\t\t3.Update mother and father's name\n\t\t\t4.Update
Student Contact No.\n\t\t\t5.Update Student CPGA");
            printf("\n\n\t\t\tEnter Your Option : ");
            scanf("%d", &choice);
            if (choice == 1)
            {
                printf("\n\t\t\tEnter Student's Name to Update: ");
                getchar();
                gets(tempInformation.studentName);
                strcpy(studentInformation.studentName,
tempInformation.studentName);
                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");
    }
    printf("\n\n\t\t\tEnter any keys to continue.......");
    getch();
}
void deleteInfo()
{
    FILE *fileOne = fopen("studentInfo.txt", "rb");
    FILE *temp = fopen("temp.txt", "wb");
    Student studentInformation, tempInformation;
    int choice, flag = 0;
    if (fileOne == NULL || temp == NULL)
    {
        printf("\n\t\t\tError !\n");
    }
    system("cls");
    printf("\t\t\t\t====== Delete Student Information ======\n");
    printf("\n\t\t\tEnter Student's ID : ");
    getchar();
    gets(tempInformation.studentId);
    while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
    {
        if (strcmp(studentInformation.studentId, tempInformation.studentId) == 0)
        {
             flag++;
             printf("\n\t\t\tAre you sure to delete ??\n\t\t\t\t1.Yes\n\t\t\t\
t2.Back\n\t\t\t\tEnter Your Option : ");
             scanf("%d", &choice);
             if (choice == 1)
             {
                  printf("\n\n\t\t\tInformation has been deleted successfully!\n\n");
             }
             else if (choice == 2)
             {
                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
             }
             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");
    }
    printf("\n\n\t\t\tEnter any keys to continue.......");
    getch();
}
void searchInfo()
{
    FILE *fileOne = fopen("studentInfo.txt", "rb");
    Student studentInformation;
    int choice, flag = 0;
    char studentID[20], studentGrade[10];
    if (fileOne == NULL)
    {
        printf("\n\t\t\tError !\n");
    }
    system("cls");
    printf("\t\t\t\t====== Search Student Information ======\n");
    printf("\n\t\t\tChoose your option :\n\t\t\t1.Search by Student ID\n\t\t\
t2.Search by Student Grade.");
    printf("\n\n\t\t\tEnter Your Option : ");
    scanf("%d", &choice);
    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);
    printf("\n\n\n\t\t\tEnter any keys to continue.......");
    getch();
}