Hi everyone im new to C and need some help with a project ive been sat by my tutor. i need to use: A menu, Structures, File handling and Dynamic memory allocation to store the name, address, telephone number and email of x amount of people. this is what i have written so far and it isnt working. Any ideas why?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<string.h>
int main (void)
{
struct database
{
char fname[20],lname[20],email[35], address[100], tel_number[15];
};
unsigned i,s;
FILE *datafile;
struct database input; /*pointer declared*/
printf("How many people? ");
scanf("%u",&s);
if(((struct database *)malloc(s * sizeof(struct database)))==NULL)
{
printf("Cannot allocate %u bytes ",s);
getch();
return 1;
}
for(i=0;i<s;i++)
{
// open the file in write mode
datafile = fopen ("data","w");
if (datafile == NULL)
{
fprintf(stderr, "\nError Writing Data\n\n");
exit (1);
}
// get details
printf("\nFirst Name: ");
gets(input.fname);
printf("Last Name: ");
gets (input.lname);
printf("Telephone Number: +44");
gets(input.tel_number);
printf("Email Address: ");
gets (input.email);
printf("Home Address: ");
gets(input.address);
// write data in file
fwrite (&input, sizeof(struct database), 1, datafile);
}
for(i=0;i<s;i++)
{
printf ("\nName = %s %s Telephone = +44%s Email Address = %s Home Address = %s",
input.fname, input.lname, input.tel_number, input.email, input.address);
}
getch();
return 0;
}/