Implementing a Student Information Management System in C

Implementing a Student Information Management System in C

In this article, we will explore how to build a simple student information management system using C. This system can store and manage basic information about students, such as name, student ID, age, etc. For beginners, this is a great exercise that can help you master the fundamentals of data structures and file operations.

1. Project Requirement Analysis

We need to implement the following functionalities:

  1. Add student information.
  2. Display all student information.
  3. Search for student information (by student ID).
  4. Delete student information (by student ID).
  5. Save to file and read from file.

2. Main Data Structure

To facilitate the storage of each student’s information, we define a <span>Student</span> structure to hold the relevant fields.

typedef struct {    char name[50];    int id;    int age;} Student;

3. Functional Module Design

Next, we will write functions for different functional modules:

1. Add Student Information

The user adds new student records by inputting data, which is stored in an array.

void addStudent(Student students[], int *count) {    if (*count < MAX_STUDENTS) {        printf("Enter name: ");        scanf("%s", students[*count].name);        printf("Enter student ID: ");        scanf("%d", &students[*count].id);        printf("Enter age: ");        scanf("%d", &students[*count].age);                (*count)++;    } else {        printf("Maximum storage capacity reached.
");    }}

2. Display All Student Information

Iterate through the array and display each added student record.

void displayStudents(Student students[], int count) {    for (int i = 0; i < count; i++) {        printf("Name: %s, Student ID: %d, Age: %d
",                students[i].name,                students[i].id,                students[i].age);    }}

3. Find Specific Student Information

Search for the corresponding student record by student ID. If found, output the relevant information; if not found, provide a prompt message.

void findStudent(Student students[], int count, int id) {    for (int i = 0; i < count; i++) {        if (students[i].id == id) {            printf("Found: Name: %s, Student ID: %d, Age: %d
",                   students[i].name,                   students[i].id,                   students[i].age);            return;        }    }        printf("Record with this student ID not found.
");}

4. Delete Specified Student Information

Delete the corresponding record in the array by comparing IDs, ensuring no gaps are left, and updating the total count value.

void deleteStudent(Student students[], int *count, int id) {    for (int i = 0; i < *count; i++) {        if (students[i].id == id) {            for (int j = i; j < (*count - 1); j++) {                 // Move forward to cover the position to be deleted                 students[j] = students[j + 1];             }             (*count)--; // Update total count value            printf("Successfully deleted record with ID %d.
", id);              return;      }   }   printf("Student ID not found, deletion failed.
");}

5. Save and Load Data

To persist our data, we can choose to write it to a text file and load data from it. This part is slightly more complex but equally important. You can use <span>fopen</span>, <span>fprintf</span>, <span>fscanf</span> to accomplish this:

Save to File:

void saveToFile(Student students[], int count) {   FILE *file = fopen("students.txt", "w");   if (!file) { // Check if the file opened successfully        perror("Unable to open file");       return;   }   for(int i=0;i<count;i++){       fprintf(file,"%s,%d,%d\n",students[i].name,students[i].id,students[i].age);   }   fclose(file); // Close the file }

Load from File:

void loadFromFile(Student students[], int *count){     FILE *file = fopen ("students.txt","r");      if(!file){            perror ("Unable to open file");            return ;      }   while(fscanf(file,"%49[^,],%d,%d\n",               &students[*count ].name ,&students[*count ].id ,&students[*count ].age )==3){          (*count)++;            }      fclose(file);         }

4. Main Menu Logic Implementation

Integrate the above methods to create the main function, allowing users to call different operations through menu selection.

#define MAX_STUDENTS 100  
int main() {       Student studentArray[MAX_STUDENTS];         int totalRegisteredStudents=0 ;            loadFromFile(studentArray,&totalRegisteredStudents );     while(1){                  printf("\n***** Student Management System *****\n");                 printf("1 - Add Student Information\n");                   printf ("2 - Display All Student Information \n" );                  printf("3 - Search for Student Information \n" );          printf ("4 - Delete Student Information \n");   printf("5 - Save to Database\n");   printf("q - Quit\n");   // Add logic for user input and function calls here  }
}

Leave a Comment