Can anybody help me? I am stuck in a c program

I have given an assignment of c to write a program that manages the library records using file handling. I have created almost four modules (Entry, Reporting/Reading, Searching, Modifying). When I execute my program, it works fine with one record but when I enter more than one record and want to read all of them, it only shows me the last record. It doesn't show me all the records.I have made a lot of changes, I put getch(); at the end of reporting loop, I opened the file in "rb+" mode(before it was opened in "rb" mode)

Also there is a problem with searching and modification that it does not recognize my file. it display message no record found.

Tell me if anybody can help me in this problem.
I am stuck in a c program
Tell Tron to get you back out!

On a serious note, we can't help you unless you give us something to work with.
Here is my program

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Regestration
{
	char name[30], f_name[30];
	char NIC[15];
	char b_name[100];
	long int  b_no;
	char i_date[10], r_date[10];
}reg;
struct Book
{
	char book_name[100];
	long b_id;
}bk;

void main()
{
	void Enter(void);
	void Report(void);
	void Search(void);
	void Modify(void);
	void Delete(void);
	void invalid(void);
	char choice;
	while(1)
	{
		clrscr();
		gotoxy(18,6);
		printf("************ WELCOME TO MAIN MENU ************");
		gotoxy(27,9);
		printf("For ENTRY Menu, Press E");
		gotoxy(27,11);
		printf("For REPORTING Menu, Press R");
		gotoxy(27,13);
		printf("For SEARCHING Menu, Press S");
		gotoxy(27,15);
		printf("For MODIFICATION Menu, Press M");
		gotoxy(27,17);
		printf("For DELETION Menu, Press D");
		gotoxy(27,19);
		printf("Please Enter Your Choice: [ ]");
		gotoxy(54,19);
		choice = getche();
		if(choice == 'E' || choice == 'e')
			Enter();
		else if(choice == 'R' || choice == 'r')
			Report();
		else if(choice == 'S' || choice == 's')
			Search();
		else if(choice == 'M' || choice == 'm')
			Modify();
		else if(choice == 'D' || choice == 'd')
			Delete();
		else if(choice == 'X' || choice == 'x')
			exit(0);
		else
			invalid();
	}
	getch();
}
void invalid(void)
{
clrscr();
gotoxy(15,5);
printf("You Have Entered An Invalid Character");
gotoxy(15,7);
printf("Press Any Key To Go Back To The Main Menu");
getch();
}
void Enter(void)
{
	char choice;
	clrscr();
	gotoxy(6,3);
	printf("If You Want To Enter Records In Book Register, Press B");
	gotoxy(6,5);
	printf("And If You Want To Enter Records In Personal Info Register, Press P");
	gotoxy(6, 7);
	printf("Please Enter Your Choice: [ ]");
	gotoxy(33,7);
	choice = getche();
	if(choice == 'B' || choice == 'b')
	{
		clrscr();
		FILE *fbook;
		fbook = fopen("Book","ab");
		if(fbook==NULL)
		printf("Could Not Open");
		int j=1;
		for(int i=1;i<=3;i++)
		{
			gotoxy(20,j);
			printf("Enter Book Name: ");
			fflush(stdin);
			gets(bk.book_name);
			fflush(stdin);
			j=j+1;
			gotoxy(20,j);
			printf("Enter Book ID Or Call Number: ");
			scanf("%d", &bk.b_id);
			j=j+1;
		}
		fwrite(&bk,sizeof(bk),1,fbook);
		fclose(fbook);
	}
	else if(choice == 'P' || choice == 'p')
	{
		clrscr();
		int i,j=3;
		FILE *freg;
		freg = fopen("Library","ab");
		if(freg==NULL)
		printf("Could Not Open");
		gotoxy(25,3);
		for(i=1;i<=3;i++)
		{
			clrscr();
			gotoxy(27,2);
			printf("Enter The Name: ");
			fflush(stdin);
			gets(reg.name);
			fflush(stdin);
			gotoxy(27,j);
			printf("Enter Father's Name: ");
			fflush(stdin);
			gets(reg.f_name);
			fflush(stdin);
			gotoxy(27,j+1);
			printf("Enter National Identity Card Number: ");
			fflush(stdin);
			gets(reg.NIC);
			fflush(stdin);
			gotoxy(27,j+2);
			printf("Enter Book Name: ");
			fflush(stdin);
			gets(reg.b_name);
			fflush(stdin);
			gotoxy(27,j+3);
			printf("Enter Book ID or Call No: ");
			scanf("%d", &reg.b_no);
			gotoxy(27,j+4);
			fflush(stdin);
			printf("Enter Issuence Date: ");
			fflush(stdin);
			gets(reg.i_date);
			fflush(stdin);
			gotoxy(27,j+5);
			printf("Enter Returned Date: ");
			fflush(stdin);
			gets(reg.r_date);
			j=3;
		}
	fwrite(&reg,sizeof(reg),1,freg);
	fclose(freg);
	}
	else
	{
	clrscr();
	gotoxy(35,4);
	printf("INVALID INPUT");
	}
	getch();
}
void Report(void)
{
	clrscr();
	char choice;
	gotoxy(7,3);
	printf("If You Want To See Records From Book Register, Press B");
	gotoxy(7,5);
	printf("And If You Want To See Records From Public Info Register, Press P");
	gotoxy(7,7);
	printf("Please Enter Your Choice: [ ]");
	gotoxy(34,7);
	choice = getche();
		if(choice == 'B' || choice =='b')
		{
			clrscr();
			FILE *fbook;
			fbook = fopen("Book","rb");
			while(fread(&bk,sizeof(bk),1,fbook)==1)
			{
				int j=1;
				gotoxy(20,j);
				printf("Book Name: ");
				fflush(stdin);
				puts(bk.book_name);
				fflush(stdin);
				j=j+1;
				gotoxy(20,j);
				printf("Book ID Or Call Number: %d", bk.b_id);
				j=j+1;
				getch();
			}
				fclose(fbook);
		}
		else if( choice == 'P' || choice =='p')
		{
			int k=1;
			clrscr();
			FILE *freg;
			freg = fopen("Library","rb");
			while(fread(&reg,sizeof(reg),1,freg)==1)
			{
				gotoxy(27,k);
				printf("Name is: ");
				puts(reg.name);
				gotoxy(27,k+1);
				printf("Father's Name is: ");
				puts(reg.f_name);
				gotoxy(27,k+2);
				printf("NIC Number is: ");
				puts(reg.NIC);
				gotoxy(27,k+3);
				printf("Book Name Is ");
				puts(reg.b_name);
				gotoxy(27,k+4);
				printf("Book Call Number is: %d", reg.b_no);
				gotoxy(27,k+5);
				printf("Book Issuece Date Is: ");
				puts(reg.i_date);
				gotoxy(27,k+6);
				printf("Book returned Date is: ");
				puts(reg.r_date);
				k++;
				getch();
			}
		fclose(freg);
		}
		else
		{
			clrscr();
			gotoxy(30,5);
			printf("INVALID INPUT");
		}
	getch();
}
void Search(void)
{
	clrscr();
	char choice;
	void Book(void);
	void P_Info(void);
	gotoxy(7,3);
	printf("If You Want To Search In Book Records, Press B");
	gotoxy(7,5);
	printf("And If You Want To Search In Public Information Records, Press P");
	gotoxy(7, 7);
	printf("Please Enter Your Choice: [ ]");
	gotoxy(34,7);
	choice = getche();
		if(choice == 'B' || choice =='b')
			Book();
		else if( choice == 'P' || choice =='p')
			P_Info();
	getch();
}
void Book(void)
{
	clrscr();
	FILE *fbook;
	int ser;
	gotoxy(13,3);
	printf("Enter Book ID or Call Number To See Its Full Record: ");
	scanf("%d", &ser);
	fbook = fopen("Book","rb");
		if(fbook==NULL)
	printf("Could Not Open");
		if(ser==bk.b_id)
		{
			gotoxy(25,6);
			printf("Book Name Is: ");
			puts(bk.book_name);
			gotoxy(25,8);
			printf("Book ID Or Call No. Is: %d",bk.b_id);
		}
		else
		{
			gotoxy(25,6);
			printf("No Record Found");
		}
	getch();
}
void P_Info(void)
{
	char ser[15];
	int k=3;
	clrscr();
	gotoxy(23,2);
	printf("Enter The NIC Number To See Full Reocrd: ");
	scanf("%d", &ser);
	FILE *freg;
	freg = fopen("Library","rb");
	while(fread(&reg,sizeof(reg),1,freg)==1);
	{
		if(ser != reg.NIC)
		{
			gotoxy(23,5);
			printf("No Record Found");
		}
		else
		{
			gotoxy(23,k);
			printf("Your Name is: ");
			puts(reg.name);
			gotoxy(23,k+1);
			printf("Father's Name is: ");
			puts(reg.f_name);
			gotoxy(23,k+2);
			printf("Yor NIC Number is: ");
			puts(reg.NIC);
			gotoxy(23,k+3);
			printf("Book Name is: %s");
			puts(reg.b_name);
			gotoxy(23,k+4);
			printf("Book Call Number is: %d", reg.b_no);
			gotoxy(23,k+5);
			printf("Book Issuece Date Is: ");
			puts(reg.i_date);
			gotoxy(23,k+6);
			printf("Book returned Date is: ");
			puts(reg.r_date);
			k=3;
		}

	}
	fclose(freg);
	getch();
}
void Modify(void)
{
	clrscr();
	char ser[15],mod=sizeof(reg);
	int j=3;
	gotoxy(23,2);
	printf("Enter The NIC Number To Modify Its Reocrd: ");
	scanf("%d", &ser);
	FILE *freg;
	freg = fopen("Library","rb+");
	while(fread(&reg,sizeof(reg),1,freg)==1);
	{
		if(ser==reg.NIC)
		{
			gotoxy(27,2);
			printf("Enter The Name: ");
			fflush(stdin);
			gets(reg.name);
			fflush(stdin);
			gotoxy(27,j);
			printf("Enter Father's Name: ");
			fflush(stdin);
			gets(reg.f_name);
			fflush(stdin);
			gotoxy(27,j+1);
			printf("Enter Book Name: ");
			fflush(stdin);
			gets(reg.b_name);
			fflush(stdin);
			gotoxy(27,j+2);
			printf("Enter Book ID or Call No: ");
			scanf("%d", &reg.b_no);
			gotoxy(27,j+3);
			fflush(stdin);
			printf("Enter Issuence Date: ");
			fflush(stdin);
			gets(reg.i_date);
			fflush(stdin);
			gotoxy(27,j+4);
			printf("Enter Returned Date: ");
			fflush(stdin);
			gets(reg.r_date);
			j=3;
		}
		else
			printf("Record Not Found");
	}
	fseek(freg,-mod,SEEK_CUR);
	fwrite(&reg,sizeof(reg),1,freg);
	fclose(freg);
	getch();
}
void Delete(void)
{
	getch();
}
Last edited on
Edit your post and put the code [code]between code tags[/code] so it has syntax highlighting. You should also recopypaste it because the tabbing got removed.
Hi

It is very hard to follow your code, however, if your code work for a single entry , it means you are overwriting the existing data when you try to enter more entries.

I think you should use an array of your classes to store the data in, for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int max_number = 100;

	Regestration 	reg[max_number];
	Book			books[max_number];

	int count = 0 ;
	while(count < 100 ){

		reg[ count ].name = "some name";
		reg[count].i_date = "some date";
		// and other things

		// the same for books
		books[count].b_id = 12;//some id
		books[count].book_name = "some book name";
		     count++;
	}
So you may use array in your loop
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
for(i=0;i<=3;i++)
{
		clrscr();
		gotoxy(27, 2);
		printf("Enter The Name: ");
		fflush (stdin);
		gets(reg.name);
		fflush(stdin);
		gotoxy(27, j);
		printf("Enter Father's Name: ");
		fflush(stdin);
		gets(reg[i].f_name);
		fflush(stdin);
		gotoxy(27, j + 1);
		printf("Enter National Identity Card Number: ");
		fflush(stdin);
		gets(reg[i].NIC);
		fflush(stdin);
		gotoxy(27, j + 2);
		printf("Enter Book Name: ");
		fflush(stdin);
		gets(reg[i].b_name);
		fflush(stdin);
		gotoxy(27, j + 3);
		printf("Enter Book ID or Call No: ");
		scanf("%d", &reg.b_no);
		gotoxy(27, j + 4);
		fflush(stdin);
		printf("Enter Issuence Date: ");
		fflush(stdin);
		gets(reg[i].i_date);
		fflush(stdin);
		gotoxy(27, j + 5);
		printf("Enter Returned Date: ");
		fflush(stdin);
		gets(reg[i].r_date);
		j = 3;
	}		


I think it should work than :-)
Last edited on
I took array for the structure, it only shows me correct output when I see records just after entering data. But when I again execute it, it just show me the messages written in double quotes of printf() statement, not the data. I think when I execute it again, the data in "library" file lost every time.
I think there is a problem with file because the library file is of 198 bytes and book file is of 208 bytes. I have just two records in book file but I have 3 records in library file. So the library file should be larger than book file in size. but it is not.
Topic archived. No new replies allowed.