Code:
/* By Luke Sowersby */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct Details get_details(int);
void print_details(struct Details);
int file_save(int, char [], struct Details *);
int file_load(char [], struct Details *);
void delete_details (int position,struct Details *fileptr);
void edit_details (int position,struct Details *fileptr);
struct Details
{
char surname[20];
char forename[20];
char address[20];
int telephone;
char email[20];
};
int main (void)
{
unsigned s;
struct Details *p;
int position=0;
int option;
printf("How many sets of details?\n");
scanf("%u",&s);
if((p=(struct Details *)malloc(s * sizeof(struct Details)))==NULL)
{
printf("Cannot allocate %u bytes",s);
getch();
return 1;
}
do
{
printf("\nPlease select one of the following options for the data:\n1. Enter\n2. Edit\n3. Delete\n4. Display\n5. Save\n6. Load\n7. Quit\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\nS=%d, position=%d",s,position);
if (position<=(s-1))
{
get_details(position);
position++;
}
else
{
printf("\nMemory full");
}
break;
case 2:
edit_details(position,p);
break;
case 3:
delete_details(position,p);
break;
case 4:
print_details(p[position]);
break;
case 5:
file_save(s,"datafile.txt",p);
break;
case 6:
s=file_load("datafile.txt",p);
position=s;
break;
case 7:
printf("\nExit");
break;
};
}
while(option!=7);
printf("\n\nHit a key");
getch();
free(p);
return 0;
}
struct Details get_details(int position)
{
struct Details get_member;
printf("\nDetails for class member %d",position+1);
printf("\nEnter the forename of the member: ");
scanf("%s",get_member.forename);
printf("\nEnter the surname of the member: ");
scanf("%s",get_member.surname);
printf("\nEnter the street address of the member: ");
scanf("%s",get_member.address);
printf("\nEnter the telephone number of the birth of the member: ");
scanf("%d",&get_member.telephone);
printf("\nEnter the email of the member: ");
scanf("%s",get_member.email);
return get_member;
}
void print_details(struct Details print_member)
{
printf("\n %s %s %s %d %s",print_member.forename,print_member.surname,print_member.address,print_member.telephone,print_member.email);
}
int file_save(int size,char filename[],struct Details *fileptr)
{
int i;
FILE *fptr;
if((fptr=fopen(filename,"wb"))==NULL)
{
printf("File cannot be opened\n");
exit(1);
return 0;
}
fwrite(fileptr,sizeof(struct Details),size,fptr);
fclose(fptr);
return 1;
}
int file_load(char filename[],struct Details *fileptr)
{
FILE *fptr;
int size;
if((fptr=fopen(filename,"rb"))==NULL)
{
printf("File cannot be opened\n");
exit(1);
return 0;
}
fread(fileptr,sizeof(struct Details),size,fptr);
fclose(fptr);
return 1;
}
void delete_details (int position,struct Details *fileptr)
{
int file_load(char filename[],struct Details *fileptr);
}
void edit_details (int position,struct Details *fileptr)
{
int file_save(int size,char filename[],struct Details *fileptr);
}
I've highlighted the key areas which i think may be causing this problem.