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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable : 4996)
void addMember(struct Member members);
void searchMember(struct Member members);
void modifyMember(struct Member members);
void displayAll(struct Member members);
struct Member { char memberId[6], name[40], gender, IC[15], contactNum[15]; };
FILE* fPtr;
main() {
struct Member members = { 0 };
int choice;
do {
printf("Member Management System\n");
printf("-------------------------\n");
printf("1. Add members info\n");
printf("2. Search members info\n");
printf("3. Modify members info\n");
printf("4. Display all members info\n");
printf("0. Exit\n");
printf("Select an option:> ");
scanf(" %d", &choice);
switch (choice) {
case 1:
addMember(members);
break;
case 2:
searchMember(members);
break;
case 3:
modifyMember(members);
break;
case 4:
displayAll(members);
break;
}
} while (choice != 0);
printf("Bye Bye!\n");
system("pause");
}
void addMember(struct Member members) {
printf("Add Member Details\n");
fPtr = fopen("member.bin", "wb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-1);
}
char ansEdit;
do {
printf("Enter your member ID: ");
scanf(" %[^\n]", &members.memberId);
rewind(stdin);
printf("Enter your name: ");
scanf(" %[^\n]", &members.name);
rewind(stdin);
printf("Enter your gender: ");
scanf(" %c", &members.gender);
rewind(stdin);
printf("Enter your IC: ");
scanf(" %[^\n]", &members.IC);
rewind(stdin);
printf("Enter your contact number: ");
scanf(" %[^\n]", &members.contactNum);
fwrite(&members, sizeof(members), 1, fPtr);
printf("Member Details\n");
printf("---------------------------------------------------------------------\n");
printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
printf("---------------------------------------------------------------------\n");
printf("%s\t%s\t\t\t%c\t\t%s\t\t%s\n", members.memberId, members.name, members.gender, members.IC, members.contactNum);
printf("Do you want to continue adding? (Y/N) ");
scanf(" %c", &ansEdit);
ansEdit = toupper(ansEdit);
} while (ansEdit == 'Y');
fclose(fPtr);
}
void searchMember(struct Member members) {
printf("Search Member Details\n");
char searchID[6], ansSearch;
do {
fPtr = fopen("member.bin", "rb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-2);
}
printf("Member ID > ");
scanf(" %[^\n]", &searchID);
rewind(stdin);
printf("Member Details\n");
printf("---------------------------------------------------------------------\n");
printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
printf("---------------------------------------------------------------------\n");
fread(&members, sizeof(members), 1, fPtr);
while (!feof(fPtr)) {
if (strcmp(searchID, members.memberId) == 0) {
printf("%s\t\t%s\t\t\t%c\t\t%s\t\t%s\n", members.memberId, members.name, members.gender, members.IC, members.contactNum);
}
fread(&members, sizeof(members), 1, fPtr);
}
printf("Do you want to continue searching? (Y/N) ");
scanf(" %c", &ansSearch);
ansSearch = toupper(ansSearch);
fclose(fPtr);
} while (ansSearch == 'Y');
}
void modifyMember(struct Member members) {
printf("Modify Member Details\n");
char ansEdit, searchID[6];
char arr[20];
fPtr = fopen("member.bin", "rb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-3);
}
fread(&members, sizeof(members), 1, fPtr);
while (fread(&members, sizeof(members), 1, fPtr)) {
for (int i = 0; i < 20; i++) {
arr[i] = '\0';
}
}
fclose(fPtr);
fPtr = fopen("member.bin", "wb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-4);
}
do {
printf("Enter ID of member to view for edit: ");
scanf(" %[^\n]", &searchID);
rewind(stdin);
if (strcmp(searchID, members.memberId) == 0) {
printf("Enter your name: ");
scanf(" %[^\n]", &members.name);
rewind(stdin);
printf("Enter your gender: ");
scanf(" %c", &members.gender);
rewind(stdin);
printf("Enter your IC: ");
scanf(" %[^\n]", &members.IC);
rewind(stdin);
printf("Enter your contact number: ");
scanf(" %[^\n]", &members.contactNum);
fwrite(&members, sizeof(members), 1, fPtr);
printf("\n");
printf("Member Details\n");
printf("---------------------------------------------------------------------\n");
printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
printf("---------------------------------------------------------------------\n");
printf("%s\t%s\t\t\t%c\t\t%s\t\t%s\n", members.memberId, members.name, members.gender, members.IC, members.contactNum);
}
printf("Do you want to continue editing? (Y/N) ");
scanf(" %c", &ansEdit);
ansEdit = toupper(ansEdit);
fclose(fPtr);
} while (ansEdit == 'Y');
}
void displayAll(struct Member members) {
printf("Display Member Details\n");
fPtr = fopen("member.bin", "rb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-5);
}
printf("Member Details\n");
printf("---------------------------------------------------------------------\n");
printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
printf("---------------------------------------------------------------------\n");
fread(&members, sizeof(members), 1, fPtr);
while (!feof(fPtr)) {
printf("%s\t\t%s\t\t\t%c\t\t%s\t\t%s\n", members.memberId, members.name, members.gender, members.IC, members.contactNum);
fread(&members, sizeof(members), 1, fPtr);
}
fclose(fPtr);
}
|