Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #define LEN 56
- #define ID_LEN 7
- typedef struct {
- char fullName[LEN];
- char id[ID_LEN];
- float price;
- int code;
- } Member;
- Member* InputMember(Member* members, unsigned size);
- float AveragePrice(Member* members, unsigned count);
- void WriteInBinFile(Member* members, int size);
- void ReadBinFile(char id[]);
- void main()
- {
- unsigned size = 0;
- Member* members = (Member*)malloc(size * sizeof(Member));
- /*unsigned count;
- printf("Enter count: ");
- scanf("%d", &count);
- fflush(stdin);
- for (unsigned i = 0; i < count; i++)
- {
- members = InputMember(members, i);
- }
- float avg = AveragePrice(members, count);
- printf("Avg: %.2f\n", avg);
- WriteInBinFile(members, count);*/
- char searchedId;
- printf("Enter searched id: ");
- scanf("%s", &searchedId);
- ReadBinFile(&searchedId);
- }
- void ReadBinFile(char id[])
- {
- FILE* fp = fopen("members.bin", "rb");
- if (fp == NULL)
- {
- perror("Error reading bin!");
- exit(2);
- }
- Member member;
- bool isFound = false;
- int len;
- while (true)
- {
- if (fread(&len, sizeof(int), 1, fp) != 1)
- {
- if (!feof(fp))
- {
- perror("Error reading len!");
- exit(3);
- }
- else
- {
- break;
- }
- }
- if (fread(member.fullName, sizeof(char), LEN - 1, fp) != LEN - 1)
- {
- perror("Error reading name!");
- exit(1);
- }
- if (fread(member.id, sizeof(char), ID_LEN - 1, fp) != ID_LEN - 1)
- {
- perror("Error reading id!");
- exit(1);
- }
- if (fread(&member.price, sizeof(float), 1, fp) != 1)
- {
- perror("Error reading price!");
- exit(1);
- }
- if (fread(&member.code, sizeof(float), 1, fp) != 1)
- {
- perror("Error reading code!");
- exit(1);
- }
- if (strcmp(member.id, id) == 0)
- {
- isFound = true;
- break;
- }
- }
- if (isFound)
- {
- printf("Bin Names: %s\n", member.fullName);
- printf("Bin ID: %s\n", member.id);
- printf("Bin M_Price: %.2f\n", member.price);
- printf("Bin Locker: %d\n", member.code);
- }
- else
- {
- printf("No such data...");
- }
- }
- void WriteInBinFile(Member* members, int size)
- {
- FILE* fp = fopen("members.bin", "wb");
- if (fp == NULL)
- {
- perror("Error opening bin!");
- exit(2);
- }
- for (int i = 0; i < size; i++)
- {
- Member member = members[i];
- int len = strlen(member.fullName);
- fwrite(&len, sizeof(int), 1, fp);
- fwrite(member.fullName, sizeof(char), LEN - 1, fp);
- fwrite(member.id, sizeof(char), ID_LEN - 1, fp);
- fwrite(&member.price, sizeof(float), 1, fp);
- fwrite(&member.code, sizeof(int), 1, fp);
- }
- fclose(fp);
- }
- float AveragePrice(Member* members, unsigned count)
- {
- float totalPrice = 0;
- float avgPrice = 0;
- for (unsigned i = 0; i < count; i++)
- {
- totalPrice += members[i].price;
- }
- avgPrice = totalPrice / count;
- for (unsigned i = 0; i < count; i++)
- {
- if (members[i].price < avgPrice)
- {
- printf("%s - %s - %.2f\n", members[i].fullName, members[i].id, members[i].price);
- }
- }
- return avgPrice;
- }
- Member* InputMember(Member* members, unsigned size)
- {
- FILE* fp = fopen("membersText.txt", "a");
- if (fp == NULL)
- {
- perror("Error opening txt file!");
- exit(1);
- }
- unsigned newSize = size + 1;
- Member* newArray = (Member*)realloc(members, newSize * sizeof(Member));
- Member member;
- char temp;
- scanf("%c",&temp);
- printf("Full Name: ");
- fgets(member.fullName, LEN, stdin);
- member.fullName[strlen(member.fullName) - 1] = '\0';
- fflush(stdin);
- printf("id: ");
- fgets(member.id, ID_LEN, stdin);
- member.id[strlen(member.id)] = '\0';
- fflush(stdin);
- printf("Price: ");
- scanf("%f", &member.price);
- fflush(stdin);
- printf("Code: ");
- scanf("%d", &member.code);
- fflush(stdin);
- newArray[size] = member;
- int len = strlen(member.fullName);
- fprintf(fp, "%d;%s;%s;%.2f;%d\n", len, member.fullName, member.id, member.price, member.code);
- fclose(fp);
- return newArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement