#include<stdlib.h>
#include<string.h>
#define MAX 10000
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];
}addr_list[MAX];
struct addr temp;
struct addr comp;
int counter;
int lend=1;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(int left, int right);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
void swap(int i, int j);
int main(void)
{
int choice;
char s[3];
counter = 0;
init_list();
for(;;){
printf("\n");
printf("1. Create a record\n");
printf("2. Delete a record\n");
printf("3. List the file\n");
printf("4. Save the file\n");
printf("5. Load the file\n");
printf("6. Sort the file\n");
printf("7. Search\n");
printf("8. Output to a text file for printing or import into wp\n");
printf("9. Quit\n");
do{
printf("\nEnter your choice: ");
readLine(s);
choice=atoi(s);
if(choice== 1) enter();
if(choice==2) delete();
if(choice==3) list();
if(choice==4) save();
if(choice==5) load();
if(choice==6) sort(1, counter);
if(choice==7) search();
if(choice== 8) txtoutput();
if(choice== 9) exit(0);
}while(choice < 1 && choice >9);
}
return 0;
}
void init_list(void)
{
register int t;
for(t=0; t<MAX; ++t) addr_list[t].name[0] ='\0';
}
void enter(void)
{
char s[40];
int i;
for(i=1; i<MAX; i++)
if(!*addr_list[i].name)
break;
if(i==MAX)
{
printf("Subscriber array full\n");
return;
}
printf("Enter information on new customer\n");
printf("Enter name: ");
readLine(addr_list[i].name);
printf("Enter street: ");
readLine(addr_list[i].street);
printf("Enter district / parish: ");
readLine(addr_list[i].district);
printf("Enter town / city: ");
readLine(addr_list[i].town);
printf("Enter county: ");
readLine(addr_list[i].county);
printf("Enter country: ");
readLine(addr_list[i].country);
printf("Enter postcode: ");
readLine(addr_list[i].postcode);
printf("Enter telephone number: ");
readLine(addr_list[i].telno);
printf("Enter email address: ");
readLine(addr_list[i].email);
counter=counter+1;
}
void delete(void)
{
int t;
int u;
printf("enter a record number #: ");
scanf("%i", &t);
getchar();
for(u=t; u<=counter; u++){
addr_list[u] = addr_list[u+1];
}
addr_list[counter].name[0] = '\0';
addr_list[counter].street[0] = '\0';
addr_list[counter].district[0] = '\0';
addr_list[counter].town[0] = '\0';
addr_list[counter].county[0] = '\0';
addr_list[counter].country[0] = '\0';
addr_list[counter].postcode[0] = '\0';
addr_list[counter].telno[0] = '\0';
addr_list[counter].email[0] = '\0';
counter = counter -1;
}
void list(void)
{
register int t;
int count;
count=0;
for(t=1; t<=counter; t++) {
count = count +1;
printf("Record number: %i\n",count);
printf("%s\n", addr_list[t].name);
printf("%s\n", addr_list[t].street);
printf("%s\n", addr_list[t].district);
printf("%s\n", addr_list[t].town);
printf("%s\n", addr_list[t].county);
printf("%s\n", addr_list[t].country);
printf("%s\n", addr_list[t].postcode);
printf("%s\n", addr_list[t].telno);
printf("%s\n", addr_list[t].email);
printf("\n");
}
}
void sort(int left, int right)
{
int i, last;
i=left;
if(left>=right) /*do nothing if array contains less than*/
return; /*two elements*/
swap(left, (left+right)/2); /*move partition element to*/
last = left; /*addr_list[0]*/
for(i=left; i<=right; i++) /*partition*/
if(strcmp(addr_list[i].name,addr_list[left].name)<0)
swap(++last, i);
swap(left, last); /*restore partition element*/
sort(left, last - 1);
sort(last+1, right);
}
void swap(int i, int j)
{
temp = addr_list[i];
addr_list[i]=addr_list[j];
addr_list[j]=temp;
i++; j--;
}
void save(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(i=1; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i],
sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");
fclose(fp);
}
void loadbackup(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}
init_list();
for(i=1; i<MAX; i++){
if(fread(&addr_list[i],
sizeof(struct addr), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter+1;
}
fclose(fp);
}
void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}
init_list();
for(i=1; i<MAX; i++){
if(fread(&addr_list[i],
sizeof(struct addr), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter +1;
}
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+");
for(t=1; t<MAX; t++) {
if(addr_list[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", addr_list[t].name);
fprintf(stream,"%s\n", addr_list[t].street);
fprintf(stream,"%s\n", addr_list[t].district);
fprintf(stream,"%s\n", addr_list[t].town);
fprintf(stream,"%s\n", addr_list[t].county);
fprintf(stream,"%s\n", addr_list[t].country);
fprintf(stream,"%s\n", addr_list[t].postcode);
fprintf(stream,"%s\n", addr_list[t].telno);
fprintf(stream,"%s\n", addr_list[t].email);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}
void outputtwo(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(i=1; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i],
sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");
fclose(fp);
}
void search(void)
{
int low, mid, top, found,count,countdown;
char s[40];
printf("\nEnter a name.\n");
readLine(s);
low = 1;
top = counter;
do
{
found = 0;
mid = (low + top)/2;
if (strcmp(s, addr_list[mid].name)==0)
{
printf("\nRecord number: %i\n", mid);
printf("\nName: %s\n",addr_list[mid].name);
printf("\nStreet: %s\n",addr_list[mid].street);
printf("\nDistrict: %s\n",addr_list[mid].district);
printf("\nTown: %s\n",addr_list[mid].town);
printf("\nCounty: %s\n",addr_list[mid].county);
printf("\nCountry: %s\n",addr_list[mid].country);
printf("\nPostcode:%s\n",addr_list[mid].postcode);
printf("\nTelephone number: %s\n",addr_list[mid].telno);
printf("\nEmail address: %s\n",addr_list[mid].email);
printf("\n");
/*break;*/
count = mid;
countdown = mid;
do
{
countdown=countdown-1;
if(strcmp(s, addr_list[countdown].name)==0)
{
printf("\nRecord number: %i\n", countdown);
printf("\nName: %s\n",addr_list[countdown].name);
printf("\nStreet: %s\n",addr_list[countdown].street);
printf("\nDistrict: %s\n",addr_list[countdown].district);
printf("\nTown: %s\n",addr_list[countdown].town);
printf("\nCounty: %s\n",addr_list[countdown].county);
printf("\nCountry: %s\n",addr_list[countdown].country);
printf("\nPostcode:%s\n",addr_list[countdown].postcode);
printf("\nTelephone number: %s\n",addr_list[countdown].telno);
printf("\nEmail address: %s\n",addr_list[countdown].email);
printf("\n");
}
}while(strcmp(s,addr_list[countdown].name)==0);
do{
count=count +1;
if(strcmp(s, addr_list[count].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",addr_list[count].name);
printf("\nStreet: %s\n",addr_list[count].street);
printf("\nDistrict: %s\n",addr_list[count].district);
printf("\nTown: %s\n",addr_list[count].town);
printf("\nCounty: %s\n",addr_list[count].county);
printf("\nCountry: %s\n",addr_list[count].country);
printf("\nPostcode:%s\n",addr_list[count].postcode);
printf("\nTelephone number: %s\n",addr_list[count].telno);
printf("\nEmail address: %s\n",addr_list[count].email);
printf("\n");
}
}while(strcmp(s,addr_list[count].name)==0);
return;
}
else
if(strcmp(s, addr_list[mid].name)<0)
top = mid -1;
else
if(strcmp(s, addr_list[mid].name)>0)
low = mid + 1;
}while(low<=top);
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}