Hi I am doing a project using binary files.I've wrote a small bit of code with no compile errors.I'm using Dev-C++ compiler(If you know a better free one please tell me)When I run it the command prompt opens and closes straight away and I don't know why This has happened me before but I need this project done VERY SOON!! can anyone help?!?This is the code I've written.
It's planned to be a banking system If anyone has Ideas for this I would also like to hear them.Thanks for listening. I did a bit of debugging and came up with this,
in the load() method here ->
Code:
1.
#include <stdio.h>
2.
#include <string.h>
3.
#include <stdlib.h>
4.
5.
6.
#define MAX 100
7.
8.
9.
int menu(void);
10.
int display(int i);
11.
void customer_search(void);
12.
void AccNo_search(void);
13.
void enter(void);
14.
void save(void);
15.
void load(void);
16.
17.
struct catalog
18.
{
19.
char name[80];
20.
char AccNo[6];
21.
char address[80];
22.
unsigned date;
23.
unsigned char month;
24.
25.
}*cat[MAX];
26.
27.
struct bank
28.
{
29.
char BankName[20];
30.
unsigned LastAccNum;
31.
unsigned sortCode;
32.
33.
}*bank[MAX];
34.
35.
int top = 0;
36.
37.
38.
int main(void)
39.
{
40.
int choice;
41.
42.
load();
43.
44.
do
45.
{
46.
choice = menu();
47.
48.
switch(choice)
49.
{
50.
case 1: enter();
51.
break;
52.
53.
case 2: customer_search();
54.
break;
55.
56.
case 3: AccNo_search();
57.
break;
58.
59.
case 4: save();
60.
}
61.
}
62.
63.
while(choice !=5);
64.
system("PAUSE");
65.
return 0;
66.
67.
}
68.
69.
70.
/* Return a menu selection*/
71.
72.
int menu(void)
73.
{
74.
int i;
75.
char str[80];
76.
77.
printf("\t\n\aEircom Billing System\n\n");
78.
printf("1->Add Customer\n");
79.
printf("2->Search by Name\n");
80.
printf("3->Search by Account Number\n");
81.
printf("4->Save added Customers\n");
82.
printf("5->Quit\n");
83.
84.
do
85.
{
86.
printf("Make your selection: ");
87.
gets(str);
88.
i = atoi(str);
89.
printf("\n");
90.
} while(i < 1 || i > 5);
91.
92.
return i;
93.
}
94.
95.
/*Enter customer into file */
96.
97.
98.
void enter(void)
99.
{
100.
int i;
101.
char temp[80];
102.
103.
104.
for (i = top; i<MAX ; i++)
105.
{
106.
107.
cat[i] = malloc(sizeof(struct catalog));
108.
109.
if(!cat[i])
110.
{
111.
printf("Out Of Memory\n");
112.
return;
113.
}
114.
115.
printf("Enter customer name (ENTER to Quit):");
116.
gets(cat[i] -> name);
117.
118.
if(!*cat[i] -> name)
119.
break;
120.
121.
printf("\nEnter AccNo: ");
122.
gets(cat[i] -> AccNo);
123.
124.
printf("\nEnter address: ");
125.
gets(cat[i] -> address);
126.
127.
printf("\nEnter year: ");
128.
gets(temp);
129.
cat[i] -> date = (unsigned) atoi(temp);
130.
131.
printf("\nEnter month: ");
132.
gets(temp);
133.
cat[i] -> month = (unsigned) atoi(temp);
134.
135.
136.
137.
}
138.
139.
top =i ;
140.
141.
}
142.
143.
/* Search for customer */
144.
145.
void customer_search(void)
146.
{
147.
char name[80];
148.
int i , found;
149.
150.
printf("Name: ");
151.
gets(name);
152.
153.
found = 0;
154.
155.
for (i=0; i<top; i++)
156.
{
157.
if (!strcmp(name,cat[i]->name))
158.
{
159.
display(i);
160.
found = 1;
161.
printf("\n");
162.
}
163.
}
164.
165.
if(!found)
166.
printf("Not Found\n");
167.
168.
}
169.
170.
171.
172.
void AccNo_search(void)
173.
{
174.
char AccNo[80];
175.
int i , found;
176.
177.
printf("AccNo: ");
178.
gets(AccNo);
179.
180.
found = 0;
181.
182.
for (i=0; i<top; i++)
183.
{
184.
if (!strcmp(AccNo,cat[i] -> AccNo))
185.
{
186.
display(i);
187.
found = 1;
188.
printf("\n");
189.
}
190.
}
191.
192.
if(!found)
193.
printf("Not Found\n");
194.
}
195.
196.
197.
198.
/*Display entries*/
199.
200.
int display(int i)
201.
{
202.
system("CLS");
203.
printf("%s\n",cat[i]->AccNo);
204.
printf("Account Holder Name: %s\n",cat[i]->name);
205.
printf("address: %s\n",cat[i]->address);
206.
printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
207.
}
208.
209.
/*load file*/
210.
211.
void load(void)
212.
{
213.
FILE *fp,*fp2;
214.
int i;
215.
216.
if((fp =fopen("index.bin","rb"))==NULL )//|| fp2 =fopen("bank.bin","rb"))==NULL)
217.
{
218.
printf("File does not exist on disk");
219.
return;
220.
}
221.
222.
223.
if(fread(&top,sizeof top,1,fp) !=1)
224.
{
225.
printf("Error Reading count");
226.
exit(1);
227.
}
228.
229.
for (i=0 ; i < top ; i++)
230.
{
231.
cat[i] = malloc(sizeof(struct catalog));
232.
233.
if (!cat[i])
234.
{
235.
printf("Out of Memory->\n");
236.
top = i-1;
237.
break;
238.
}
239.
240.
if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
241.
{
242.
printf("Error reading catalog data->\n");
243.
exit(1);
244.
}
245.
}
246.
247.
248.
249.
for (i=0 ; i < top ; i++)
250.
{
251.
bank[i] = malloc(sizeof(struct bank));
252.
253.
if (!bank[i])
254.
{
255.
printf("Out of Memory->\n");
256.
top = i-1;
257.
break;
258.
}
259.
260.
if(fread(bank[i], sizeof(struct bank),1,fp) !=1)
261.
{
262.
printf("Error reading catalog data->\n");
263.
exit(1);
264.
}
265.
}
266.
fclose(fp2);
267.
fclose(fp);
268.
}
269.
270.
271.
/* Save the catalog file */
272.
273.
void save(void)
274.
{
275.
FILE *fp;
276.
int i;
277.
278.
if((fp =fopen("index.bin","w+"))==NULL)
279.
{
280.
printf("Cant open file->\n");
281.
exit(1);
282.
}
283.
if(fwrite(&top,sizeof top,1,fp) !=1)
284.
{
285.
printf("Error Writting count.\n");
286.
exit(1);
287.
}
288.
289.
for(i=0 ; i < top ; i++)
290.
{
291.
if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
292.
printf("Error writing count.\n");
293.
exit(1);
}
fclose(fp);
}
Can Anyone help PLEASE!!!!!!