Advertisement
vallec

Untitled

May 31st, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4.  
  5. #define LEN 56
  6. #define ID_LEN 7
  7.  
  8. typedef struct {
  9. char fullName[LEN];
  10. char id[ID_LEN];
  11. float price;
  12. int code;
  13. } Member;
  14.  
  15. Member* InputMember(Member* members, unsigned size);
  16.  
  17. float AveragePrice(Member* members, unsigned count);
  18.  
  19. void WriteInBinFile(Member* members, int size);
  20.  
  21. void ReadBinFile(char id[]);
  22.  
  23. void main()
  24. {
  25. unsigned size = 0;
  26. Member* members = (Member*)malloc(size * sizeof(Member));
  27.  
  28. /*unsigned count;
  29. printf("Enter count: ");
  30. scanf("%d", &count);
  31. fflush(stdin);
  32.  
  33. for (unsigned i = 0; i < count; i++)
  34. {
  35. members = InputMember(members, i);
  36. }
  37.  
  38. float avg = AveragePrice(members, count);
  39. printf("Avg: %.2f\n", avg);
  40.  
  41. WriteInBinFile(members, count);*/
  42. char searchedId;
  43. printf("Enter searched id: ");
  44. scanf("%s", &searchedId);
  45. ReadBinFile(&searchedId);
  46. }
  47.  
  48. void ReadBinFile(char id[])
  49. {
  50. FILE* fp = fopen("members.bin", "rb");
  51.  
  52. if (fp == NULL)
  53. {
  54. perror("Error reading bin!");
  55. exit(2);
  56. }
  57. Member member;
  58. bool isFound = false;
  59. int len;
  60.  
  61. while (true)
  62. {
  63. if (fread(&len, sizeof(int), 1, fp) != 1)
  64. {
  65. if (!feof(fp))
  66. {
  67. perror("Error reading len!");
  68. exit(3);
  69. }
  70. else
  71. {
  72. break;
  73. }
  74. }
  75.  
  76. if (fread(member.fullName, sizeof(char), LEN - 1, fp) != LEN - 1)
  77. {
  78. perror("Error reading name!");
  79. exit(1);
  80. }
  81.  
  82. if (fread(member.id, sizeof(char), ID_LEN - 1, fp) != ID_LEN - 1)
  83. {
  84. perror("Error reading id!");
  85. exit(1);
  86. }
  87.  
  88. if (fread(&member.price, sizeof(float), 1, fp) != 1)
  89. {
  90. perror("Error reading price!");
  91. exit(1);
  92. }
  93.  
  94. if (fread(&member.code, sizeof(float), 1, fp) != 1)
  95. {
  96. perror("Error reading code!");
  97. exit(1);
  98. }
  99.  
  100. if (strcmp(member.id, id) == 0)
  101. {
  102. isFound = true;
  103. break;
  104. }
  105. }
  106.  
  107. if (isFound)
  108. {
  109. printf("Bin Names: %s\n", member.fullName);
  110. printf("Bin ID: %s\n", member.id);
  111. printf("Bin M_Price: %.2f\n", member.price);
  112. printf("Bin Locker: %d\n", member.code);
  113. }
  114. else
  115. {
  116. printf("No such data...");
  117. }
  118. }
  119.  
  120. void WriteInBinFile(Member* members, int size)
  121. {
  122. FILE* fp = fopen("members.bin", "wb");
  123.  
  124. if (fp == NULL)
  125. {
  126. perror("Error opening bin!");
  127. exit(2);
  128. }
  129.  
  130. for (int i = 0; i < size; i++)
  131. {
  132. Member member = members[i];
  133. int len = strlen(member.fullName);
  134.  
  135. fwrite(&len, sizeof(int), 1, fp);
  136. fwrite(member.fullName, sizeof(char), LEN - 1, fp);
  137. fwrite(member.id, sizeof(char), ID_LEN - 1, fp);
  138. fwrite(&member.price, sizeof(float), 1, fp);
  139. fwrite(&member.code, sizeof(int), 1, fp);
  140. }
  141.  
  142. fclose(fp);
  143. }
  144.  
  145. float AveragePrice(Member* members, unsigned count)
  146. {
  147. float totalPrice = 0;
  148. float avgPrice = 0;
  149.  
  150. for (unsigned i = 0; i < count; i++)
  151. {
  152. totalPrice += members[i].price;
  153. }
  154.  
  155. avgPrice = totalPrice / count;
  156.  
  157. for (unsigned i = 0; i < count; i++)
  158. {
  159. if (members[i].price < avgPrice)
  160. {
  161. printf("%s - %s - %.2f\n", members[i].fullName, members[i].id, members[i].price);
  162. }
  163. }
  164.  
  165. return avgPrice;
  166. }
  167.  
  168. Member* InputMember(Member* members, unsigned size)
  169. {
  170. FILE* fp = fopen("membersText.txt", "a");
  171.  
  172. if (fp == NULL)
  173. {
  174. perror("Error opening txt file!");
  175. exit(1);
  176. }
  177.  
  178. unsigned newSize = size + 1;
  179. Member* newArray = (Member*)realloc(members, newSize * sizeof(Member));
  180.  
  181. Member member;
  182. char temp;
  183. scanf("%c",&temp);
  184.  
  185. printf("Full Name: ");
  186. fgets(member.fullName, LEN, stdin);
  187. member.fullName[strlen(member.fullName) - 1] = '\0';
  188. fflush(stdin);
  189.  
  190. printf("id: ");
  191. fgets(member.id, ID_LEN, stdin);
  192. member.id[strlen(member.id)] = '\0';
  193. fflush(stdin);
  194.  
  195. printf("Price: ");
  196. scanf("%f", &member.price);
  197. fflush(stdin);
  198.  
  199. printf("Code: ");
  200. scanf("%d", &member.code);
  201. fflush(stdin);
  202.  
  203. newArray[size] = member;
  204.  
  205. int len = strlen(member.fullName);
  206.  
  207. fprintf(fp, "%d;%s;%s;%.2f;%d\n", len, member.fullName, member.id, member.price, member.code);
  208.  
  209. fclose(fp);
  210.  
  211. return newArray;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement