Developing a Student Information Management System in C

Developing a Student Information Management System in C

In this article, we will learn how to develop a simple student information management system using C. This system allows users to add, view, and delete student information, including basic details such as name, student ID, and age. For beginners, this project is a great exercise to help you understand fundamental concepts of C, such as structures, functions, and file operations.

1. Project Overview

1.1 Functional Requirements

The student information management system needs to have the following functionalities:

  • Add student information
  • Display all stored student information
  • Delete specified student information
  • Save data to a file for future use

1.2 Technology Stack

This project primarily uses the C programming language.

2. Data Structure Design

We need to define a structure to represent student information. Below is the structure we will use:

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

The <span>Student</span> structure contains three fields: name (string), student ID (integer), and age (integer).

3. System Functionality Implementation

Next, we will implement each functionality step by step.

3.1 Adding Student Information

We first create a function that can add new student information:

void addStudent(Student *students, int *count) {    if (*count >= MAX_STUDENTS) {        printf("Reached maximum capacity\n");        return;    }    printf("Please enter name: ");    scanf("%s", students[*count].name);    printf("Please enter student ID: ");    scanf("%d", &students[*count].id);    printf("Please enter age: ");    scanf("%d", &students[*count].age);    (*count)++;}

Here, we pass the array and counter by pointer, allowing us to effectively update data within the function.

3.2 Displaying All Student Information

Next, we create a function to display all stored student information:

void displayStudents(Student *students, int count) {    if (count == 0) {        printf("No student records to display\n");        return;    }    for (int i = 0; i < count; i++) {        printf("Name: %s, Student ID: %d, Age: %d\n",                students[i].name,               students[i].id,               students[i].age);   }}

At this point, if there are no records, the user will be prompted, and no error output will occur.

3.3 Deleting Specified Student Information

The operation to delete a specific entry is as follows:

void deleteStudent(Student *students, int *count) {   if (*count == 0) {        printf("No records to delete\n");        return;    }    int idToDelete;   printf("Please enter the student ID to delete: ");   scanf("%d", &idToDelete);   for (int i = 0; i < *count; i++) {        if (students[i].id == idToDelete) {            for (int j = i; j < (*count - 1); j++) {                students[j] = students[j + 1]; // Shift elements left to cover the deleted item            }                           (*count)--; // Update the counter             printf("Successfully deleted record with student ID %d.\n", idToDelete);                    return;               }        }    printf("Student ID not found.\n"); }

This method first searches for the matching ID to delete, then appropriately decreases the count and updates the stored data.

Saving and Loading Data to/from File

To make our data persistent, the code is as follows:

Saving Data to File

void saveData(Student *students, int count){FILE* file = fopen("students.dat","wb");   if(file==NULL){   printf("\nUnable to open file!\n");   return;}   fwrite(&count, sizeof(int),1,file);  fwrite(students,sizeof(Student), count,file); fclose(file);  printf("\nSave successful!\n");}

Loading Data from File

void loadData(Student* students ,int* count){   FILE* file= fopen ("students.dat","rb");if(file==NULL){      printf("\nUnable to read file!!\n");       return;}fread(&(*count),sizeof(int),1,file );fread(students ,sizeof(Student),(*count),file );  fclose(file );      printf("Data loaded successfully!\n");}

Finally, the main program flow control is as follows:

#define MAX_STUDENTS 100 // Maximum limit of students is 100 int main () {    Student students[MAX_STUDENTS];    int count = 0;    while(1){       // Main program logic here    }}

Please note that this code is not perfect and can be further improved, such as adding input validation mechanisms, better error handling, and optimizing the user interface. This is just to allow beginners to practice various fundamental knowledge.

Leave a Comment