-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.c
72 lines (63 loc) · 2.22 KB
/
Student.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
#include <stdio.h>
#include <string.h> // sử dụng cho hàm strcmp
#include "Student.h"
//Part c
void getStudentInfo(Student_detail *AnewStudent) {
puts("Enter student ID: ");
scanf("%*c%19s", AnewStudent->student_ID);
puts("Enter student name: ");
scanf("%*c%19[^\n]", AnewStudent->student_name); //đọc vào cả chuỗi (bao gồm dấu cách), chỉ dừng khi gặp \n
puts("Enter student address: ");
scanf("%*c%19[^\n]", AnewStudent->student_address);
puts("Enter student major: ");
scanf("%*c%19[^\n]", AnewStudent->student_major);
}
//Part d
void showStudentInfo(Student_detail Students[], int n) { //In từ mảng Students
puts("List of students");
printf("%-20s%-20s%-20s%-20s\n", "ID", "Name", "Address", "Major");
puts("--------------------------------------------------------------------------------------");
for (int i = 0; i < n; i++) {
printf("%-20s%-20s%-20s%-20s\n", Students[i].student_ID, Students[i].student_name, Students[i].student_address,
Students[i].student_major);
}
}
//Part e
//hàm tìm vị trí của một học sinh trong danh sách Students[] theo mã ID học sinh
int indexFromStudentID(Student_detail Students[], int number_of_students, char ID_to_find[]) {
for (int i = 0; i < number_of_students; i++) {
int match = strcmp(ID_to_find, Students[i].student_ID);
if (match == 0) {
return i;
}
}
}
//Part l //Ctrl + S
void writeStudentFile(char file_name[], Student_detail Students[], int n) {
FILE *fp;
fp = fopen(file_name, "wb"); //mỗi lần ghi mới lại toàn bộ danh sách học sinh
for (int i = 0; i < n; i++) {
fwrite(&Students[i], sizeof(Student_detail), 1, fp);
}
puts("Write students data successfully !");
fclose(fp);
}
//Part m
void readStudentFile(char file_name[], Student_detail Students[], int *n) {
FILE *fp;
fp = fopen(file_name, "rb");
if (fp == NULL) {
printf("Cannot open file %s\n", file_name);
return;
}
int i = 0;
while (!feof(fp)) {
fread(&Students[i], sizeof(Student_detail), 1, fp);
if (feof(fp)) {
break;
}
i++;
}
*n = i;
fclose(fp);
}