i have written a database program for a course i am doing and there is one major bug.
when loading a saved file. and then displaying that data characters are added to the data. if i then load the file again, most of the characters disapear, however not all of them.
unfortunatly im not sure where the problem lyes so i feel i should post the entire code and header file.
Code:
#include <stdio.h> /*included library files*/
#include <conio.h>
#include <alloc.h>
#include <stdlib.h>
#include <C:\my documents\database.h>
void main(void) /*main program*/
{
unsigned menu,i,entry=0,del,edit; /*declare variables*/
struct details *p;
char ans;
char file_name [file_name_length];
clrscr();
flushall();/*clears stdin(and all streams)*/
/*allocates memory for database, and returns error if not available*/
if((p = (struct details *)calloc(1, (sizeof(struct details))))==NULL)
{
printf("Cannot allocate memory");
getch();
}
do
{
printf("\n1. Enter data\n2. Edit data\n3. Delete data\n4. Display data\n5. Save data\n6. Load data\n7. Quit");
printf("\nSelect an option (1-7): ");/*request input*/
scanf("%u",&menu);
realloc(p,entry);
switch(menu) /*set menu as a switch function*/
{
case 1: /*if menu=1 this code will be executed*/
printf("\nEnter Details:");
do
{
p[entry]= enter_details(entry);
printf("Enter more detials? y/n: ");
ans=getchar();
entry++;
}
while (ans!='n');
break;
case 2: flushall();/*if menu=2 this code will be executed*/
clrscr();
printf("\nEdit Data:\nEnter number of record to edit: ");
scanf("%u",&edit);
printf("\nRecord Number:%u",edit);
print_details(p[(edit-1)]);
printf("\nPress a key to edit this record");
getch();
p[(edit-1)]=enter_details((edit-1));
break;
case 3: flushall();/*if menu=3 this code will be executed*/
printf("\nDelete Data:\nEnter number of the record you wish to delete: ");
scanf("%u",&del);
break;
case 4: flushall();/*if menu=4 this code will be executed*/
clrscr();
printf("\nDisplay Data:\n");
i=0;
do
{
printf("\nRecord Number:%u",(i+1));
print_details(p[i]);
i++; }
while (i<entry);
break;
case 5: flushall();/*if menu=5 this code will be executed*/
printf("\nSave Data:\nEnter full Filename: ");
gets(file_name);
if ((save_data(entry,file_name,p))==1)
printf("\nFile saved successfully\n");
else printf("\nError: File not saved\b");
break;
case 6: flushall();/*if menu=6 this code will be executed*/
printf("\nLoad Data:\nWARNING:All unsaved data will be lost\nEnter full Filename: ");
gets(file_name);
entry=(load_data(file_name,(p)));
printf("%u records loaded",entry);
break;
}
}
while (menu!=7);
clrscr();
printf("Press any key to exit");
getch();
free(p);
}
and the header file
Code:
#define name_length 15 /*defined data lengths*/
#define address_length 30
#define email_length 25
#define telephone_length 20
#define file_name_length 20
struct details enter_details(int); /*function prototypes*/
void print_details(struct details);
int save_data(int, char [], struct details *);
int load_data(char [], struct details *);
struct details /*Structure format*/
{
char surname[name_length];
char forename[name_length];
char address[address_length];
char telephone[telephone_length];
char email[email_length];
};
struct details enter_details(int i)
{
struct details get_member;
flushall();
printf("\nEnter Surname %d : ",(i+1));
gets(get_member.surname);
printf("\nEnter Forename %d : ",(i+1));
gets(get_member.forename);
printf("\nEnter Address %d : ",(i+1));
gets(get_member.address);
printf("\nEnter Telephone Number %d : ",(i+1));
gets(get_member.telephone);
printf("\nEnter Email Address %d : ",(i+1));
gets(get_member.email);
return get_member;
}
void print_details(struct details print_details)
{
printf("\nName: %s, %s ",print_details.surname,print_details.forename);
printf("\nAddress: %s",print_details.address);
printf("\nTelephone: %s",print_details.telephone);
printf("\nEmail: %s\n",print_details.email);
}
int save_data(int entrys, char filename[], struct details *fileptr)
{
FILE *fptr;
if((fptr = fopen(filename,"wb"))==NULL)
{
printf("File cannot be opened\n");
exit(1);
return 0;
}
fwrite(fileptr,sizeof(struct details),entrys,fptr);
fclose(fptr);
return 1;
}
int load_data(char filename[],struct details *fileptr)
{
int i=0;
FILE *fptr;
clrscr();
if((fptr = fopen(filename,"rb"))==NULL)/*open file*/
{
printf("File cannot be opened\a");
getch();
exit(1);
return NULL;
}
while(!feof(fptr)) /*while not at the end of the file*/
{
getc(fptr); /*load bit of file*/
i++; /*incrememnt size of file*/
}
i=(i/(sizeof(struct details)));
fclose(fptr);
realloc(fileptr,i);
if((fptr = fopen(filename,"rb"))==NULL)
{
printf("File cannot be opened\a");
getch();
exit(1);
return 0;
}
fread(fileptr,sizeof(struct details),i,fptr);
fclose(fptr);
return i;
}
i hope someone has the time to look throuh it for me