#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct addr{
char name[40];
char street[40];
char district[40];
char town[40];
char county[40];
char country[40];
char postcode[40];
char telno[40];
char email[40];
struct addr *next;
};
typedef struct addr address;
address *temp, *start, *pointer, *i= NULL;
void list(), add(void), deletion(void), search(void);
void save(), load(void), txtoutput(void), sort(void);
void readLine(char buffer[]);
int main(void)
{
int choice;
for(;;){
printf("\n");
printf("1. Insert a record\n");
printf("2. List the file\n");
printf("3. Delete a record\n");
printf("4. Save the file\n");
printf("5. Save to a text file for output to a wp\n");
printf("6. Search for a record\n");
printf("7. Load the file\n");
printf("8. Quit\n");
do{
printf("\nEnter your choice: ");
scanf("%i",&choice);
getchar();
if(choice==1) add();
if(choice==2) list();
if(choice==3) deletion();
if(choice==4) save();
if(choice==5) txtoutput();
if(choice==6) search();
if(choice==7) load();
if(choice== 8) exit(0);
}while(choice < 1 && choice >8);
}
return 0;
}
void add()
{
address *q;
pointer = (address*) malloc(sizeof(address));
q = (address*) malloc(sizeof(address));
printf("Enter information on new customer\n");
printf("Enter name: ");
readLine(pointer->name);
printf("Enter street: ");
readLine(pointer->street);
printf("Enter district / parish: ");
readLine(pointer->district);
printf("Enter town / city: ");
readLine(pointer->town);
printf("Enter county: ");
readLine(pointer->county);
printf("Enter country: ");
readLine(pointer->country);
printf("Enter postcode: ");
readLine(pointer->postcode);
printf("Enter telephone number: ");
readLine(pointer->telno);
printf("Enter email address: ");
readLine(pointer->email);
if (start == NULL || strcmp(pointer->name, start->name)<0)
{
pointer->next=start;
start=pointer;
}
else
{
q = start;
while(q->next!=NULL && strcmp(q->next->name , pointer->name) < 0)
q=q->next;
pointer->next=q->next;
q->next=pointer;
}
}
void list()
{
if(start==NULL)
printf("No records to view");
/*for (temp=start;temp!=NULL;temp=temp->next)*/
temp = start;
while(temp!=NULL)
{
printf("\n");
printf("%s\n",temp->name);
printf("%s\n",temp->street);
printf("%s\n",temp->district);
printf("%s\n",temp->town);
printf("%s\n",temp->county);
printf("%s\n",temp->country);
printf("%s\n",temp->postcode);
printf("%s\n",temp->telno);
printf("%s\n",temp->email);
printf("\n");
temp=temp->next;
}
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}
void save()
{
FILE *fp;
if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}
temp=start;
while(temp){
fwrite(temp,
sizeof(struct addr), 1, fp);
temp = temp->next;
}
fclose(fp);
}
void load()
{
FILE *fp;
if((fp=fopen("maillist", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}
start = ( address*) malloc(sizeof(address));
/*while(start){
temp=start->next;
free(temp);
start=temp;
}*/
free(start);
start=NULL;
while(!feof(fp)){
i = ( address*) malloc(sizeof(address));
temp = ( address*) malloc(sizeof(address));
if(!i){
printf("Out of memory.\n");
return;
}
if(1!=fread(i,sizeof(struct addr),1,fp))break;
if(start!=NULL)
{
temp = start;
while(temp->next!=NULL)
temp=temp->next;
i->next=NULL;
temp->next=i;
}
else
{
start=i;
i->next=NULL;
}
}
fclose(fp);
}
void txtoutput(void)
{
int t;
int count;
FILE *stream;
char filename[30];
count = 0;
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
stream = fopen(filename, "w+");
temp = start;
while(temp) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", temp->name);
fprintf(stream,"%s\n", temp->street);
fprintf(stream,"%s\n", temp->district);
fprintf(stream,"%s\n", temp->town);
fprintf(stream,"%s\n", temp->county);
fprintf(stream,"%s\n", temp->country);
fprintf(stream,"%s\n", temp->postcode);
fprintf(stream,"%s\n", temp->telno);
fprintf(stream,"%s\n", temp->email);
fprintf(stream,"\n");
temp=temp->next;
}
fclose(stream);
return;
}
void search()
{
int flag=0;
char name[40];
printf("Enter name of person to search for\n");
readLine(name);
temp=start;
while(temp)
{
if(strcmp(name, temp->name)==0)
{
printf("%s\n",temp->name);
printf("%s\n",temp->street);
printf("%s\n",temp->district);
printf("%s\n",temp->town);
printf("%s\n",temp->county);
printf("%s\n",temp->country);
printf("%s\n",temp->postcode);
printf("%s\n",temp->telno);
printf("%s\n",temp->email);
flag= 1;
}
temp=temp->next;
}
if(flag==0)
printf("Not found in list\n");
}
void deletion()
{
address *t, *pt;
t = ( address*) malloc(sizeof(address));
pt = ( address*) malloc(sizeof(address));
char name[40];
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Enter name of person to be deleted\n");
readLine(name);
if(strcmp(name,start->name)==0)
{
temp=start;
start = start->next;
free(temp);
printf("Record is deleted\n");
return;
}
pt=start;
t=start->next;
while(t!=NULL&& strcmp(t->name,name)!=0)
{
pt =t;
t=t->next;
}
if(t==NULL)
{
printf("Record does not exist\n");
return;
}
if(strcmp(t->name,name)==0)
{
pt->next=t->next;
printf("Record is deleted\n");
free(t);
return;
}
}
No comments:
Post a Comment