Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include "students.h"
- int load_register(Students *reg, const char *filename)
- {
- //int count = 0;
- FILE *filu = fopen(filename, "r");
- if (!filu)
- return -1;
- reg->array = NULL;
- reg->count = 0;
- while (1) {
- char namelength = 0;
- size_t s = fread(&namelength, sizeof(char), 1, filu);
- if (namelength <= 0)
- break;
- char *name = calloc((namelength + 1) * sizeof(char), 1);
- fread(name, 1, namelength, filu);
- name[namelength] = '\0';
- char id[8];
- fread(id, 1, 8, filu);
- int age = fgetc(filu);
- char course[16];
- fread(course, 1, 16, filu);
- int numpoints = fgetc(filu);
- float *points = malloc(numpoints * sizeof(float));
- fread(points, numpoints, sizeof(float), filu);
- create_student(reg, name, id, age, course);
- set_points(reg, id, course, points, numpoints);
- }
- fclose(filu);
- return reg->count;
- }
- int save_register(const Students *reg, const char *filename)
- {
- int count = 0;
- FILE *filu = fopen(filename, "w");
- if (!filu)
- return -1;
- for(unsigned int i = 0; i < reg->count; i++) {
- unsigned char length = strlen(reg->array[i].name);
- fwrite(&length, 1, 1, filu);
- fwrite(reg->array[i].name, sizeof(char),length, filu);
- fwrite(reg->array[i].id, 8, 1, filu);
- fputc(reg->array[i].age, filu);
- fwrite(reg->array[i].course, 16, 1, filu);
- fputc(reg->array[i].numPoints, filu);
- fwrite(reg->array[i].points, sizeof(float), reg->array[i].numPoints, filu);
- count++;
- }
- fclose(filu);
- return count;
- }
- src/main.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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement