Edit the member details

Hi, I have a binary file called member.bin and I want to read the data from the file and let the user edit the data.
-DATA FROM MEMBER.BIN-
Member Details
---------------------------------------------------------------------
Member ID Name Gender IC Contact Number
---------------------------------------------------------------------
M2002 Chin F 881205-14-1234 013-123 4567
M2004 Chan F 881205-14-1230 013-123 4560

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
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable : 4996)


main() {
	typedef struct { char memberId[6], name[40], gender, IC[15], contactNum[15]; } Member;
	Member members;

	FILE* fPtr;

	//add members
	printf("Add Member Details\n");
	fPtr = fopen("member.bin", "wb");
	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(Member), 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);

	//search members
	printf("Search Member Details\n");
	char searchID[6], ansSearch;

	do {
		fPtr = fopen("member.bin", "rb");

		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(Member), 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(Member), 1, fPtr);
		}

		printf("Do you want to continue searching? (Y/N) ");
		scanf(" %c", &ansSearch);
		ansSearch = toupper(ansSearch);

		fclose(fPtr);

	} while (ansSearch == 'Y');
	
	//modify members
	printf("Modify Member Details\n");

	char ansEdit;
	char arr[20];

	fPtr = fopen("member.bin", "rb");
	fread(&members, sizeof(Member), 1, fPtr);
	while (fread(&members, sizeof(Member), 1, fPtr)) {
		for (int i = 0; i < 20; i++) {
			arr[i] = '\0';

		}

	}
	fclose(fPtr);

	fPtr = fopen("member.bin", "wb");

	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(Member), 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');

	//display members
	printf("Display Member Details\n");
	fPtr = fopen("member.bin", "rb");

	printf("Member Details\n");
	printf("---------------------------------------------------------------------\n");
	printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
	printf("---------------------------------------------------------------------\n");

	fread(&members, sizeof(Member), 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(Member), 1, fPtr);
	}

	fclose(fPtr); 


	system("pause");
}

However, when I want to edit the data, it came out like this after the searching part

Modify Member Details
Enter ID of member to view for edit: M2002
Do you want to continue editing? (Y/N)

instead of this

Modify Member Details
Enter ID of member to view for edit: M2002
Enter your name: Chin
Enter your gender: F
Enter your IC: 881205-14-1234
Enter your contact number: 012-123 4567
Member Details
---------------------------------------------------------------------
Member ID    Name            Gender        IC        Contact Number
M2002        Chin            F         881205-14-1234   012-123 4567
Do you want to continue editing? (Y/N)

The above output is an example of changing one of the member's data, which is the contact number, from 013-123 4567 to 012-123 4567.
Thank you in advance for your guidance!
Your main needs to refactored.

You need three functions to
- add
- search
- edit

With those in place, it's easy to have a main() which just prints a simple 3 choice menu and asks you what to do next.

You also need a function to just input a record from the user.
Then you don't have to copy/paste code from add to edit.

rewind(stdin) probably does nothing useful.
It almost certainly doesn't do what you imagine.

@salem c
Hi, I had added the functions to my coding. Did you mean something like this?

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);
}

Last edited on
@salem c
I have some problems with the modify and display part
The program doesn't allow me to modify the member's information

Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 3
Modify Member Details
Enter ID of member to view for edit: M2002
Do you want to continue editing? (Y/N)

It doesn't allow me to change the member's name, IC or contact number.
As for the display part, it doesn't display anything on the console

Display Member Details
Member Details
---------------------------------------------------------------------
Member ID       Name                    Gender          IC              Contact Number
---------------------------------------------------------------------
The tricky bit is knowing how to open the file for update mode, and how to use fseek and fflush.
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable : 4996)

struct Member {
  char memberId[6], name[40], gender, IC[15], contactNum[15];
};

void addMember(struct Member members);
void searchMember(struct Member members);
void modifyMember(struct Member members);
void displayAll(struct Member members);
void inputMember(struct Member *members);
void printMemberBanner(void);
void printMember(struct Member members);

int 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 inputMember(struct Member *members)
{
    printf("Enter your member ID: ");
    scanf(" %[^\n]", members->memberId);
    printf("Enter your name: ");
    scanf(" %[^\n]", members->name);
    printf("Enter your gender: ");
    scanf(" %c", &members->gender);
    printf("Enter your IC: ");
    scanf(" %[^\n]", members->IC);
    printf("Enter your contact number: ");
    scanf(" %[^\n]", members->contactNum);
}

void printMemberBanner(void)
{
    printf("Member Details\n");
    printf("---------------------------------------------------------------------\n");
    printf("Member ID\tName\t\t\tGender\t\tIC\t\tContact Number\n");
    printf("---------------------------------------------------------------------\n");
}

void printMember(struct Member members)
{
    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);
}

void addMember(struct Member members)
{
  printf("Add Member Details\n");
  FILE *fPtr = fopen("member.bin", "wb");

  if (fPtr == NULL) {
    printf("Failed to open file");
    exit(-1);
  }

  char ansEdit;
  do {
    inputMember(&members);
    fwrite(&members, sizeof(members), 1, fPtr);

    printMemberBanner();
    printMember(members);

    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 {
    FILE *fPtr = fopen("member.bin", "rb");
    if (fPtr == NULL) {
      printf("Failed to open file");
      exit(-2);
    }

    printf("Member ID > ");
    scanf(" %[^\n]", searchID);

    printMemberBanner();

    while (fread(&members, sizeof(members), 1, fPtr) == 1) {
      if (strcmp(searchID, members.memberId) == 0) {
        printMember(members);
      }
    }

    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];

  FILE *fPtr = fopen("member.bin", "r+b");
  if (fPtr == NULL) {
    printf("Failed to open file");
    exit(-3);
  }

  do {
    printf("Enter ID of member to view for edit: ");
    scanf(" %[^\n]", searchID);

    rewind(fPtr);   //!! start from the beginning of the file
    while (fread(&members, sizeof(members), 1, fPtr) == 1) {
      if (strcmp(searchID, members.memberId) == 0) {
        inputMember(&members);
        fseek(fPtr, -((long)(sizeof(members))), SEEK_CUR);  //!! step back one record
        fwrite(&members, sizeof(members), 1, fPtr);         //!! write it
        fflush(fPtr);                                       //!! commit it

        printf("\n");

        printMemberBanner();
        printMember(members);
        break; // because member names are unique, no need to search more
      }
    }

    printf("Do you want to continue editing? (Y/N) ");
    scanf(" %c", &ansEdit);
    ansEdit = toupper(ansEdit);
  } while (ansEdit == 'Y');

  fclose(fPtr);
}

void displayAll(struct Member members)
{
  printf("Display Member Details\n");
  FILE *fPtr = fopen("member.bin", "rb");
  if (fPtr == NULL) {
    printf("Failed to open file");
    exit(-5);
  }

  printMemberBanner();

  while ( fread(&members, sizeof(members), 1, fPtr) == 1 ) {
    printMember(members);
  }

  fclose(fPtr);
}



$ ./a.out 
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 1
Add Member Details
Enter your member ID: M1
Enter your name: Fred
Enter your gender: M
Enter your IC: IC1
Enter your contact number: 1234
Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M1	Fred			M		IC1		1234
Do you want to continue adding? (Y/N) y
Enter your member ID: M2
Enter your name: Wilma
Enter your gender: F
Enter your IC: IC2
Enter your contact number: 4321
Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M2	Wilma			F		IC2		4321
Do you want to continue adding? (Y/N) n
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 4
Display Member Details
Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M1	Fred			M		IC1		1234
M2	Wilma			F		IC2		4321
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 2
Search Member Details
Member ID > M1
Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M1	Fred			M		IC1		1234
Do you want to continue searching? (Y/N) n
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 3
Modify Member Details
Enter ID of member to view for edit: M1
Enter your member ID: M1
Enter your name: Barney
Enter your gender: M
Enter your IC: IC3
Enter your contact number: 9999

Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M1	Barney			M		IC3		9999
Do you want to continue editing? (Y/N) n
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 4
Display Member Details
Member Details
---------------------------------------------------------------------
Member ID	Name			Gender		IC		Contact Number
---------------------------------------------------------------------
M1	Barney			M		IC3		9999
M2	Wilma			F		IC2		4321
Member Management System
-------------------------
1. Add members info
2. Search members info
3. Modify members info
4. Display all members info
0. Exit
Select an option:> 0
Bye Bye!
Topic archived. No new replies allowed.