-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent_operations.c
110 lines (93 loc) · 3.31 KB
/
student_operations.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student_operations.h"
// Function to save student records to a file
void saveStudentsToFile(const Student **students, int numStudents, const char *filename) {
FILE *file = fopen(filename, "w");
if (!file) {
perror("Unable to open file for writing");
return;
}
for (int i = 0; i < numStudents; i++) {
fprintf(file, "%s %d %.2f\n", students[i]->name, students[i]->rollNumber, students[i]->marks);
}
fclose(file);
printf("Student records saved to %s successfully.\n", filename);
}
// Function to load student records from a file
void loadStudentsFromFile(Student ***students, int *numStudents, const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Unable to open file for reading");
return;
}
*numStudents = 0; // Reset the number of students
char name[50];
int rollNumber;
float marks;
while (fscanf(file, "%49s %d %f", name, &rollNumber, &marks) == 3) {
// Allocate memory for a new student
*students = realloc(*students, (*numStudents + 1) * sizeof(Student *));
if (!(*students)) {
perror("Memory allocation failed");
fclose(file);
return;
}
(*students)[*numStudents] = malloc(sizeof(Student));
if (!(*students)[*numStudents]) {
perror("Memory allocation failed");
fclose(file);
return;
}
// Copy student data
strcpy((*students)[*numStudents]->name, name);
(*students)[*numStudents]->rollNumber = rollNumber;
(*students)[*numStudents]->marks = marks;
(*numStudents)++;
}
fclose(file);
printf("Student records loaded from %s successfully.\n", filename);
}
// Function to modify a student record
void modifyStudent(Student **students, int numStudents) {
int rollNumber;
printf("Enter the roll number of the student to modify: ");
scanf("%d", &rollNumber);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i]->rollNumber == rollNumber) {
index = i;
break;
}
}
if (index == -1) {
printf("Student with roll number %d not found.\n", rollNumber);
return;
}
// Modify student details
printf("Enter new name: ");
scanf("%49s", students[index]->name); // Limit input size
printf("Enter new marks: ");
while (scanf("%f", &students[index]->marks) != 1) {
printf("Invalid input. Please enter valid marks: ");
while (getchar() != '\n'); // Clear invalid input
}
printf("Student record updated successfully.\n");
}
// Function to search and display a student by roll number
void searchAndDisplayStudent(Student **students, int numStudents) {
int rollNumber;
printf("Enter roll number to search: ");
scanf("%d", &rollNumber);
for (int i = 0; i < numStudents; i++) {
if (students[i]->rollNumber == rollNumber) {
printf("Student found:\n");
printf("Name: %s\n", students[i]->name);
printf("Roll Number: %d\n", students[i]->rollNumber);
printf("Marks: %.2f\n", students[i]->marks);
return;
}
}
printf("Student with roll number %d not found.\n", rollNumber);
}