Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Student
- {
- char *name;
- int fac_num;
- double avg_grade;
- };
- typedef struct Student Student_t;
- struct StudentList
- {
- Student_t student;
- struct StudentList *next;
- };
- typedef struct StudentList StudentList_t;
- void delete_all(StudentList_t **head)
- {
- StudentList_t *temp;
- while(*head) {
- temp = *head;
- *head = (*head)->next;
- free(temp);
- }
- *head = NULL;
- }
- StudentList_t* read_from_binary(StudentList_t *head, FILE *fp)
- {
- Student_t student;
- StudentList_t *current;
- int res;
- while(1) {
- res = fread(&student, 1, sizeof(Student_t), fp);
- if(res < 1) {
- break;
- }
- if(head == NULL) {
- head = malloc(sizeof(StudentList_t));
- head->student = student;
- current = head;
- } else {
- current->next = malloc(sizeof(StudentList_t));
- current->next->student = student;
- current = current->next;
- }
- }
- return head;
- }
- double get_avg_grade(StudentList_t *head)
- {
- double avg = 0;
- int count = 0;
- while(head) {
- avg += head->student.avg_grade;
- count += 1;
- head = head->next;
- }
- if(count > 0) {
- avg = avg / count;
- } else {
- avg = 0;
- }
- return avg;
- }
- void write_to_bin_file(StudentList_t *head, char* file_name)
- {
- FILE *fp = fopen(file_name, "wb");
- if(fp == NULL) {
- perror("Cannot open file");
- exit(EXIT_FAILURE);
- }
- while(head) {
- fwrite(&head->student, 1, sizeof(Student_t), fp);
- head = head->next;
- }
- fclose(fp);
- }
- void insert_student(StudentList_t **head, Student_t student)
- {
- StudentList_t *temp;
- if(*head == NULL) {
- *head = malloc(sizeof(StudentList_t));
- (*head)->student = student;
- (*head)->next = NULL;
- } else {
- temp = malloc(sizeof(StudentList_t));
- temp->next = *head;
- *head = temp;
- }
- }
- int main () {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement