Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include "students.h"
  6.  
  7.  
  8. int load_register(Students *reg, const char *filename)
  9. {
  10. //int count = 0;
  11.  
  12. FILE *filu = fopen(filename, "r");
  13. if (!filu)
  14. return -1;
  15.  
  16. reg->array = NULL;
  17. reg->count = 0;
  18.  
  19. while (1) {
  20.  
  21. char namelength = 0;
  22. size_t s = fread(&namelength, sizeof(char), 1, filu);
  23.  
  24. if (namelength <= 0)
  25. break;
  26.  
  27. char *name = calloc((namelength + 1) * sizeof(char), 1);
  28. fread(name, 1, namelength, filu);
  29. name[namelength] = '\0';
  30.  
  31. char id[8];
  32. fread(id, 1, 8, filu);
  33.  
  34. int age = fgetc(filu);
  35.  
  36. char course[16];
  37. fread(course, 1, 16, filu);
  38.  
  39. int numpoints = fgetc(filu);
  40.  
  41. float *points = malloc(numpoints * sizeof(float));
  42. fread(points, numpoints, sizeof(float), filu);
  43.  
  44. create_student(reg, name, id, age, course);
  45. set_points(reg, id, course, points, numpoints);
  46.  
  47. }
  48.  
  49. fclose(filu);
  50. return reg->count;
  51.  
  52. }
  53.  
  54.  
  55. int save_register(const Students *reg, const char *filename)
  56. {
  57. int count = 0;
  58.  
  59. FILE *filu = fopen(filename, "w");
  60. if (!filu)
  61. return -1;
  62.  
  63. for(unsigned int i = 0; i < reg->count; i++) {
  64.  
  65. unsigned char length = strlen(reg->array[i].name);
  66.  
  67. fwrite(&length, 1, 1, filu);
  68. fwrite(reg->array[i].name, sizeof(char),length, filu);
  69. fwrite(reg->array[i].id, 8, 1, filu);
  70. fputc(reg->array[i].age, filu);
  71. fwrite(reg->array[i].course, 16, 1, filu);
  72. fputc(reg->array[i].numPoints, filu);
  73. fwrite(reg->array[i].points, sizeof(float), reg->array[i].numPoints, filu);
  74.  
  75. count++;
  76.  
  77. }
  78. fclose(filu);
  79. return count;
  80. }
  81. src/main.c
  82. ?
  83. 1
  84. 2
  85. 3
  86. 4
  87. 5
  88. 6
  89. 7
  90. 8
  91. 9
  92. 10
  93. 11
  94. 12
  95. 13
  96. 14
  97. 15
  98. 16
  99. 17
  100. 18
  101. 19
  102. 20
  103. 21
  104. 22
  105. 23
  106. 24
  107. 25
  108. 26
  109. 27
  110. 28
  111. 29
  112. 30
  113. 31
  114. 32
  115. 33
  116. 34
  117. 35
  118. 36
  119. 37
  120. 38
  121. 39
  122. 40
  123. 41
  124. 42
  125. 43
  126. 44
  127. 45
  128. 46
  129. 47
  130. 48
  131. 49
  132. 50
  133. 51
  134. 52
  135. 53
  136. 54
  137. 55
  138. 56
  139. 57
  140. 58
  141. 59
  142. 60
  143. 61
  144. 62
  145. 63
  146. 64
  147. 65
  148. 66
  149. 67
  150. 68
  151. 69
  152. 70
  153. 71
  154. 72
  155. #include <stdio.h>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement