-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstudent.c
More file actions
119 lines (98 loc) · 2.77 KB
/
student.c
File metadata and controls
119 lines (98 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <string.h>
#define MAX 20
#define FILE_NAME "students.txt"
struct Student {
char name[50];
int roll;
};
/* Function declarations */
void loadStudents(struct Student s[], int *count);
void saveStudents(struct Student s[], int count);
void addStudent(struct Student s[], int *count);
void viewStudents(struct Student s[], int count);
int main() {
struct Student s[MAX];
int count = 0;
int choice;
/* Load existing students from file */
loadStudents(s, &count);
while (1) {
printf("\n--- STUDENT MANAGEMENT SYSTEM ---\n");
printf("1. Add Student\n");
printf("2. View Students\n");
printf("3. Exit\n");
printf("Enter choice: ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number.\n");
while (getchar() != '\n');
continue;
}
switch (choice) {
case 1:
addStudent(s, &count);
break;
case 2:
viewStudents(s, count);
break;
case 3:
saveStudents(s, count);
printf("Student data saved. Exiting...\n");
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
}
/* Load students from file */
void loadStudents(struct Student s[], int *count) {
FILE *fp = fopen(FILE_NAME, "r");
if (fp == NULL) return;
while (fscanf(fp, "%49[^,],%d\n", s[*count].name, &s[*count].roll) == 2) {
(*count)++;
if (*count >= MAX) break;
}
fclose(fp);
}
/* Save students to file */
void saveStudents(struct Student s[], int count) {
FILE *fp = fopen(FILE_NAME, "w");
if (fp == NULL) {
printf("Error saving student data.\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%s,%d\n", s[i].name, s[i].roll);
}
fclose(fp);
}
/* Add a new student */
void addStudent(struct Student s[], int *count) {
if (*count >= MAX) {
printf("Student list is full.\n");
return;
}
printf("Enter name: ");
getchar();
fgets(s[*count].name, 50, stdin);
s[*count].name[strcspn(s[*count].name, "\n")] = '\0';
printf("Enter roll number: ");
if (scanf("%d", &s[*count].roll) != 1) {
printf("Invalid roll number.\n");
while (getchar() != '\n');
return;
}
(*count)++;
printf("Student added successfully.\n");
}
/* View all students */
void viewStudents(struct Student s[], int count) {
if (count == 0) {
printf("No students found.\n");
return;
}
printf("\n--- STUDENT LIST ---\n");
for (int i = 0; i < count; i++) {
printf("%d. %s - Roll %d\n", i + 1, s[i].name, s[i].roll);
}
}