Friday, 22 February 2019

A sorted singly linked list database in C

#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;
int numofrecords;
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.  Load the file\n");
printf("2.  List the file\n");
printf("3.  Search for a record\n");
printf("4.  Insert a record\n");
printf("5.  Delete a record\n");
printf("6.  Save the file\n");
printf("7.  Save to a text file for output to a wp\n");
printf("8.  Quit\n");
do{
printf("\nEnter your choice: ");
scanf("%i",&choice);
getchar();
if(choice==1) load();
if(choice==2) list();
if(choice==3) search();
if(choice==4) add();
if(choice==5) deletion();
if(choice==6) save();
if(choice==7) txtoutput();
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;
}
numofrecords = numofrecords + 1;
}



void list()
{
int i = 0;
if(start==NULL)
printf("No records to view");
temp = start;
while(temp!=NULL)
{
i = i + 1;
printf("\n");
printf("Record number %i\n",i);
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));

free(start);
start=NULL;
while(!feof(fp)){
numofrecords = numofrecords + 1;
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 as filename.txt.\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 searchname[40]; 
printf("Enter name of person to search for\n");
readLine(searchname);
temp=start;
while(temp)
{
if(strcmp(searchname, temp->name)==0)
{
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");
flag= 1;
}
temp=temp->next;
}
if(flag==0) 
printf("Not found in list\n");
}

void deletion()
{

address *q;

char recnumber[40];
int i, delperson;
i = 0;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Enter the record number of person to be deleted\n");
readLine(recnumber);
delperson = atoi(recnumber);
/* Delete first record*/
if(delperson==1)
{
temp=start;
start = start->next;
free(temp);
printf("Record is deleted\n");
numofrecords = numofrecords - 1;
return;
}

q = start;
/*Delete in between record*/
while(q->next->next!=NULL)
{
i = i + 1;
if(i + 1 == delperson)
{
temp = q->next;
q->next = temp->next;
free(temp);
printf("Record is deleted\n");
numofrecords = numofrecords - 1;
return;
}
q = q->next;
}
/*Delete last record*/
if(delperson + 1 == numofrecords)
{
temp = q->next;
free(temp);
q->next = NULL;
printf("Record is deleted\n");
numofrecords = numofrecords - 1;
return;
}
}



Tuesday, 28 October 2014

link to an ebook and kindlebook on simple database listings

http://cforkindle.webs.com/

A database written in C that uses a random access file.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<stdbool.h>
struct contactinfo{
char name[40];
char number[40];
char email[40];
};

struct filedetails{
int filelength;
int recnum;
};
struct filedetails filespec;
struct contactinfo person;
struct contactinfo blankfields;
FILE *details;
int flag = 0;
char contact[40];
long int offset=0, length;
long int size;
char filename[30];
bool recordfound;
void newsubs(void), search(void), readLine(char buffer[]), new(void), sort(void);
void list(void), delete(void), prntxt(void);
int main(void)
{
int choice;
char ch[3];
for(;;){
printf("\n");
printf("1. New file\n");
printf("2. New record\n");
printf("3. Sort\n");
printf("4. List\n");
printf("5. Search\n");
printf("6. Delete\n");
printf("7. Export to a txt file for printing.\n");
printf("8. Quit:\n");
do{
printf("\nChoose...");
readLine(ch);
choice = atoi(ch);
if(choice==1)new();
if(choice==2)newsubs();
if(choice==3)sort();
if(choice==4)list();
if(choice==5)search();
if(choice==6)delete();
if(choice==7) prntxt();
if(choice==8) exit(0);
}while(choice < 1 && choice > 8);
}
return 0;
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}

void newsubs()
{
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL)
{
printf("Cannot open file.\n");
}
if((details=fopen(filename, "r+b"))!=NULL) {

fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Records in file =%d\n",filespec.recnum);
printf("Size of file in bytes = %d\n",filespec.filelength);
printf("Enter information on new contact.\n");
printf("Name:\n");
readLine(person.name);
printf("Phone number:\n");
readLine(person.number);
printf("Email:\n");
readLine(person.email);
size = sizeof(struct contactinfo);
offset=(filespec.recnum +1)*size;
if(fseek(details, offset,0))
printf("Seek error.\n");
length = ftell(details);
printf("Pointer before writing at %ld\n",length);
if(fwrite(&person, sizeof(struct contactinfo), 1,details)==0) {
printf("Write error.");
}
else
{
length = ftell(details);
printf("Pointer after writing at %ld\n",length);
filespec.filelength= length;
filespec.recnum+=1;
rewind(details);
fwrite(&filespec, sizeof(struct filedetails), 1,details);
person = blankfields;
fclose(details);
}
}
}

void search()
{
int searchcount=0;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL)
flag = 0;
recordfound=0;
fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Enter a name to look for.\n");

readLine(contact);
offset=0;
while(!feof(details))
{
searchcount+=1;
size = sizeof(struct contactinfo);
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);

if(strcmp(contact, person.name)==0)
{
recordfound=1;
printf("%s\n",person.name);
printf("%s\n",person.number);
printf("%s\n",person.email);
printf("\n");
}
if(searchcount == filespec.recnum)break;
}
}
fclose(details);
if(recordfound == 0)
{
printf("Record not found.\n");
}
else
{
if(recordfound == 1) recordfound== 0;
flag = 1;
}
}
}


void new()
{
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "w+b"))==NULL) {
printf("Cannot open file.\n");
}
filespec.recnum=0;
fwrite(&filespec , sizeof(struct filedetails), 1,details);
printf("New file created\n");
fclose(details);
}


void sort()
{
int recnum;
int i;
struct contactinfo temp;
long int u;
long int v;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
fread(&filespec , sizeof(struct filedetails), 1,details);
for(u=1; u<filespec.recnum; u++)
{
for (v=u+1; v<=filespec.recnum; v++){
size = sizeof(person);
fseek(details, sizeof(person)*u,0);
fread(&person, sizeof(person), 1, details);
fseek(details, sizeof(person)*(v),0);
fread(&temp, sizeof(person), 1, details);
if(strcmp(person.name,temp.name)>0)
{
fseek(details, sizeof(person)*(u),0);
fwrite(&temp, sizeof temp, 1,details);
fseek(details, sizeof(person)*(v),0);
fwrite(&person, sizeof(person), 1,details);
}
}
}
fclose(details);
}
}

void list()
{
int counter = 0;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Records in file =%d\n",filespec.recnum);
printf("Size of file in bytes =%d\n", filespec.filelength);
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
counter +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
printf("\n");
printf("%s\n",person.name);
printf("%s\n",person.number);
printf("%s\n",person.email);
printf("\n");
if(counter == filespec.recnum)break;
}
}
fclose(details);
}
}   
   
void delete()
{   

long int offset2=0;
int recordcount;
int counter=0;
int counter2=0;
char filename2[30];
char reply;
FILE *tempfile;
search();
if(flag == 0)
{
printf("Exiting function delete.\n");
}
else
{
if((details=fopen(filename, "r+b"))==NULL)
{
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
recordcount = filespec.recnum;
printf("Enter a file name for the destination of the edited files.\n");
readLine(filename2);
if((tempfile=fopen(filename2, "w+b"))==NULL)
{
printf("Cannot open file.\n");
}
else
{
filespec.recnum=0;
fwrite(&filespec , sizeof(struct filedetails), 1,tempfile);
printf("New file created\n");
fclose(tempfile);
if((tempfile=fopen(filename2, "r+b"))!=NULL)
{
printf("File %s is open for input.\n",filename2);
}
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
person = blankfields;
counter +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
printf("%s\n",person.name);
if(strcmp(contact, person.name)!=0)
{
counter2+=1;
if(counter2 == recordcount) break;
offset2=(counter2)*size;
if(fseek(tempfile, offset2,0)){
printf("Seek error.");
}
else
{
if(fwrite(&person, sizeof(struct contactinfo), 1, tempfile));
length = ftell(tempfile);
}
}
}
}
fclose(details);
filespec.recnum=counter2-1;


filespec.filelength=length;
rewind(tempfile);
fwrite(&filespec , sizeof(struct filedetails), 1,tempfile);
fclose(tempfile);
}
}
}
}
}

void prntxt()
{
int count;
FILE *stream;
char filenametxt[30];
count = 0;
printf("Enter a 'filename.txt' file name to create and write to.\n");
readLine(filenametxt);
printf("\n");
stream = fopen(filenametxt, "w+");
if((details=fopen(filenametxt, "w+"))==NULL) {
printf("Cannot open file.\n");
}
else
{
int counter = 0;
printf("Enter a file name to read records from.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
counter +=1;
count +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", person.name);
fprintf(stream,"%s\n", person.number);
fprintf(stream,"%s\n", person.email);
fprintf(stream,"\n");
if(counter == filespec.recnum)break;
}
}
}
}
}
fclose(details);
fclose(stream);
}

Wednesday, 25 June 2014

A database in C using a singly linked list.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.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];
struct addr *next;
};
typedef struct addr address;
address *temp, *start, *pointer,  *i= NULL;

void list(), createlist(void), add(void), addafter(void), deletion(void), search(void);
void save(), load(void), txtoutput(void);
void store();
void readLine(char buffer[]);
int main(void)
{
int choice;
for(;;){
printf("\n");
printf("1.  Create a list\n");
printf("2.  Add a record to the beginning of the list\n");
printf("3.  Add a record to the end of the list\n");
printf("4.  List the file\n");
printf("5.  Delete a record\n");
printf("6.  Save the file\n");
printf("7.  Save to a text file for output to a wp\n");
printf("8.  Search for a record\n");
printf("9.  Load the file\n");
printf("10. Quit\n");
do{
printf("\nEnter your choice: ");
scanf("%i",&choice);
getchar();
if(choice== 1) createlist();
if(choice==2) add();
if(choice==3) addafter();
if(choice==4) list();
if(choice==5) deletion();
if(choice==6) save();
if(choice==7) txtoutput();
if(choice==8) search();
if(choice==9) load();
if(choice== 10) exit(0);
}while(choice < 1 && choice >10);
}
return 0;
}



void add()
{
pointer = (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 pointer: ");
readLine(pointer->email);
pointer->next=start;
start=pointer;

}

void addafter()
{
pointer = (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 pointer: ");
readLine(pointer->email);
temp=start;
while(temp->next !=NULL)
temp=temp->next;
temp->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("%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);
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 createlist()
{
pointer = (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 pointer: ");
readLine(pointer->email);
pointer->next=NULL;
if(start==NULL)/*if list is empty*/
start=pointer;
else
{
/*add element to end of list*/
temp=start;
while(temp->next !=NULL)
temp=temp->next;
temp->next =pointer;
}
}
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);
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;
}
}

Wednesday, 30 October 2013

A C database implementing a quick sort and a binary chop.

#include<stdio.h>
#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';
}

Saturday, 24 March 2012

Another version of a database written in C

A working database can be downloaded at http://tarkastatistics.webs.com/

/****************************************************************************
 *                                                                          *
 * File    : main.c                                                         *
 *                                                                          *
 * Purpose : Console mode (command line) program.                           *
 *                                                                          *
 * History : Date      Reason                                               *
 *           00/00/00  Created                                              *
 *                                                                          *
 ****************************************************************************/

#include <stdio.h>

/****************************************************************************
 *                                                                          *
 * Function: main                                                           *
 *                                                                          *
 * Purpose : Main entry point.                                              *
 *                                                                          *
 * History : Date      Reason                                               *
 *           00/00/00  Created                                              *
 *                                                                          *
 ****************************************************************************/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

typedef struct {
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];
char response[1][10];
}addr_list;

addr_list address[MAX];
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void loadbackup(void), outputthree(void);
void outputtwo(void), search(void);
void readLine(char buffer[]);
void swap(int u, int v);
int main(void)
{
int choice;
char s[80];
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.  Save to a back up file\n");
printf("9.  Load from a backup file\n");
printf("10. Output to a text file for printing or import into wp\n");
printf("11. Quit\n");
do {
printf("\nEnter your choice: ");
readLine(s);
/*gets(s);*/
choice=atoi(s);
/*scanf("%i", &choice);*/
}while(choice < 0 && choice > 11);
switch(choice) {
case 1: enter();
break;
case 2: delete();
break;
case 3: list();
break;
case 4: save();
break;
case 5: load();
break;
case 6: sort();
break;
case 7: search();
break;
case 8: outputtwo();
break;
case 9: loadbackup();
break;
case 10: outputthree();
break;
case 11: exit(0);
}
}
return 0;
}

void init_list(void)
{
register int t;

for(t=0; t<MAX; ++t) address[t].name[0] ='\0';
}

void enter(void)
{
char s[40];
int i;
for(i=1; i<MAX; i++)
if(!*address[i].name)
break;
if(i==MAX)
{
printf("Subscriber array full\n");
return;
}
printf("Enter information on new customer\n");

printf("Enter name: ");
readLine(address[i].name);

printf("Enter street: ");
readLine(address[i].street);

printf("Enter district / parish: ");
readLine(address[i].district);

printf("Enter town / city: ");
readLine(address[i].town);

printf("Enter county: ");
readLine(address[i].county);

printf("Enter country: ");
readLine(address[i].country);

printf("Enter postcode: ");
readLine(address[i].postcode);

printf("Enter telephone number: ");
readLine(address[i].telno);

printf("Enter email address: ");
readLine(address[i].email);
counter=counter+1;
}



void delete(void)
{
int t;
int u;
printf("enter a record number #: ");
scanf("%i", &t);
for(u=t; u<=counter; u++){
strncpy(address[u].name, address[u+1].name, 40);
strncpy(address[u].street, address[u+1].street, 40);
strncpy(address[u].district, address[u+1].district, 40);
strncpy(address[u].town, address[u+1].town, 40);
strncpy(address[u].county, address[u+1].county, 40);
strncpy(address[u].country, address[u+1].country, 40);
strncpy(address[u].postcode, address[u+1].postcode, 40);
strncpy(address[u].telno, address[u+1].telno, 40);
strncpy(address[u].email, address[u+1].email, 40);
}
address[counter].name[0] = '\0';
address[counter].street[0] = '\0';
address[counter].district[0] = '\0';
address[counter].town[0] = '\0';
address[counter].county[0] = '\0';
address[counter].country[0] = '\0';
address[counter].postcode[0] = '\0';
address[counter].telno[0] = '\0';
address[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", address[t].name);
printf("%s\n", address[t].street);
printf("%s\n", address[t].district);
printf("%s\n", address[t].town);
printf("%s\n", address[t].county);
printf("%s\n", address[t].country);
printf("%s\n", address[t].postcode);
printf("%s\n", address[t].telno);
printf("%s\n", address[t].email);
printf("\n");
}
}

void sort(void)
{
register int u;
register int v;
for(u=1; u<counter; u++)
for (v=u+1; v<=counter; v++){
if( strcmp(address[u].name, address[v].name) >0){
swap(u, v);
}
}
}

void swap(int u, int v)
{
addr_list temp;
temp = address[u];
address[u] = address[v];
address[v] = temp;
}

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(*address[i].name)
if(fwrite(&address[i],
sizeof(address[i]), 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(&address[i],
sizeof(address[i]), 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(&address[i],
sizeof(address[i]), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter +1;
}
fclose(fp);
}


void outputthree(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(address[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", address[t].name);
fprintf(stream,"%s\n", address[t].street);
fprintf(stream,"%s\n", address[t].district);
fprintf(stream,"%s\n", address[t].town);
fprintf(stream,"%s\n", address[t].county);
fprintf(stream,"%s\n", address[t].country);
fprintf(stream,"%s\n", address[t].postcode);
fprintf(stream,"%s\n", address[t].telno);
fprintf(stream,"%s\n", address[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(*address[i].name)
if(fwrite(&address[i],
sizeof(address[i]), 1, fp)!=1)
printf("File write error.\n");

fclose(fp);
}

void search(void)
{
int i;
int count;
char s[40];
count=0;
printf("\nEnter a name.\n");
readLine(s);
for(i=1;i<MAX;i++)
{
if(address[i].name[0]) {
count = count +1;
}

if (strcmp(s, address[i].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",address[i].name);
printf("\nStreet: %s\n",address[i].street);
printf("\nDistrict: %s\n",address[i].district);
printf("\nTown: %s\n",address[i].town);
printf("\nCounty: %s\n",address[i].county);
printf("\nCountry: %s\n",address[i].country);
printf("\nPostcode:%s\n",address[i].postcode);
printf("\nTelephone number: %s\n",address[i].telno);
printf("\nEmail address: %s\n",address[i].email);
printf("\n");
}
}
}

void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}

Thursday, 20 October 2011

A database written in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define MAX 10000

struct person {
char initials[10];
char surname[40];
char name[40];
char telno[40];
}person_list[MAX];

struct person temp;
int counter;

void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void);
void sort(void);
void loadbackup(void), outputtwo(void), loadbackuptwo(void);
void copy(void);
void search(void);
void searchbysurname(void);
int find_free(void);

int main(void)
{
int choice;
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.  Copy the file after saving\n");
printf("8.  Load the copied file\n");
printf("9.  Search by initials\n");
printf("10. Search by surname\n");
printf("11. Save with a filename of your choice\n");
printf("12. Load from a filename of your choice\n");
printf("13. Quit\n");
do {
printf("\nEnter your choice: ");
scanf("%i",&choice);
}while(choice < 1 && choice > 13);
if(choice==1) enter();
if(choice==2) delete();
if(choice==3) list();
if(choice==4) save();
if(choice==5) load();
if(choice==6)sort();
if(choice==7) copy();
if(choice==8) loadbackup();
if(choice==9) search();
if(choice==10) searchbysurname();
if(choice==11) outputtwo();
if(choice==12) loadbackuptwo();
if(choice==13) exit(1);
}
return 0;
}

void init_list(void)
{
register int t;

for(t=1; t<MAX; ++t) person_list[t].initials[0] ='\0';
}


void enter(void)
{
int slot;
char s[80];

slot = find_free();

if(slot==-1) {
printf("\nList Full");
return;
}

printf("\nEnter initials: ");
scanf("%s",person_list[slot].initials);
printf("\nEnter surname: ");
scanf("%s",person_list[slot].surname);
printf("\nEnter first name: ");
scanf("%s",person_list[slot].name);
printf("\nEnter telephone number: ");
scanf("%s",person_list[slot].telno);

counter = counter +1;
}

int find_free(void)
{
register int t;

for(t=1; person_list[t].initials[0] && t<MAX; t++)

if(t==MAX) return -1;
return t;
}

void delete(void)
{
int recnum;
printf("enter a record number #: ");
scanf("%i", &recnum);
person_list[recnum].initials[0] = '\0';
}

void list(void)
{
register int t;
int count;
count = 0;
for(t=1; t<MAX; t++) {
if(person_list[t].initials[0]) {
count = count +1;
printf("Record number: %i\n",count);
printf("Initials: %s\n", person_list[t].initials);
printf("Surname: name %s\n", person_list[t].surname);
printf("First name: %s\n", person_list[t].name);
printf("Telephone number: %s\n", person_list[t].telno);
printf("\n");
}
}
printf("\n\n");
}

void sort(void)
{
register int u;
register int t;
for (t=1; t<counter; t++){
for (u=t+1; u<=counter; u++)
if( strcmp(person_list[t].initials, person_list[u].initials) >0){
strncpy(temp.initials, person_list[t].initials, 10);
strncpy(temp.surname, person_list[t].surname, 40);
strncpy(temp.name, person_list[t].name, 40);
strncpy(temp.telno, person_list[t].telno, 40);
strncpy(person_list[t].initials, person_list[u].initials, 10);
strncpy(person_list[t].surname, person_list[u].surname, 40);
strncpy(person_list[t].name, person_list[u].name, 40);
strncpy(person_list[t].telno, person_list[u].telno, 40);
strncpy(person_list[u].initials, temp.initials, 10);
strncpy(person_list[u].surname, temp.surname, 40);
strncpy(person_list[u].name, temp.name, 40);
strncpy(person_list[u].telno, temp.telno, 40);

}
}
}

void save(void)
{
int t;
FILE *stream;
stream = fopen("personlist1.txt", "w+");
for(t=1; t<MAX; t++) {
if(person_list[t].initials[0]) {
fprintf(stream,"%i\n",t);
fprintf(stream,"%s\n", person_list[t].initials);
fprintf(stream,"%s\n", person_list[t].surname);
fprintf(stream,"%s\n", person_list[t].name);
fprintf(stream,"%s\n", person_list[t].telno);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}


void load(void)
{
int t;
char i[MAX];

FILE *stream;
init_list();
stream = fopen("personlist1.txt", "r+");
t=0;
do
{
t=t+1;
fscanf(stream,"%s\n",i);
fscanf(stream,"%s\n", person_list[t].initials);
fscanf(stream, "%s\n", person_list[t].surname);
fscanf(stream, "%s\n", person_list[t].name);
fscanf(stream, "%s\n", person_list[t].telno);
fscanf(stream, "\n");
counter=counter+1;
}while(!feof(stream));
fclose(stream);
return;
}

void copy(void)
{
FILE *in, *out;
int c;
if ((in = fopen ("personlist1.txt", "r")) ==NULL){
printf("Cant open %s file for reading.\n","cdlist");
return 1;
}

if ((out = fopen("personlist2.txt", "w")) ==NULL){
printf("Cant open %s for writing.\n", "cdlistbackup");
return 2;
}

while ((c = getc (in)) !=EOF)
putc (c, out);

fclose (in);
fclose(out);
printf("File has been copied.\n");
return 0;
}


void loadbackup(void)
{
int t;
char i[MAX];

FILE *stream;
init_list();
stream = fopen("personlist2.txt", "r+");
t=0;
do
{
t=t+1;
fscanf(stream,"%s\n",i);
fscanf(stream,"%s\n", person_list[t].initials);
fscanf(stream, "%s\n", person_list[t].surname);
fscanf(stream, "%s\n", person_list[t].name);
fscanf(stream, "%s\n", person_list[t].telno);
fscanf(stream, "\n");
}while(!feof(stream));
fclose(stream);
return;
}




void search(void)
{
int i, count;
char s[30];
count=0;
printf("\nEnter an initial.\n");
scanf("%s",s);
for(i=1;i<MAX;i++)
{
if(person_list[i].initials[0]) {
count = count +1;
}
if (strcmp(s, person_list[i].initials)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nInitials: %s\n",person_list[i].initials);
printf("\nSurname: %s\n",person_list[i].surname);
printf("\nFirst name: %s\n",person_list[i].name);
printf("\nTelephone number: %s\n",person_list[i].telno);
printf("\n");
}
}
}

void searchbysurname(void)
{
int i, count;
char s[30];
count =0;
printf("\nEnter a surname.\n");
scanf("%s",s);
for(i=1;i<MAX;i++)
{
if(person_list[i].initials[0]) {
count = count +1;
}
if (strcmp(s, person_list[i].surname)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nInitials: %s\n",person_list[i].initials);
printf("\nSurname: %s\n",person_list[i].surname);
printf("\nFirst name: %s\n",person_list[i].name);
printf("\nTelephone number: %s\n",person_list[i].telno);
printf("\n");
}
}
}

void outputtwo(void)
{
FILE *stream;
register int t;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((stream=fopen(filename, "w+"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(t=1; t<MAX; t++) {
if(person_list[t].initials[0]) {
fprintf(stream,"%i\n",t);
fprintf(stream,"%s\n", person_list[t].initials);
fprintf(stream,"%s\n", person_list[t].surname);
fprintf(stream,"%s\n", person_list[t].name);
fprintf(stream,"%s\n", person_list[t].telno);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}


void loadbackuptwo(void)
{
FILE *stream;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
register int t;
char i[MAX];
if((stream=fopen(filename, "r+"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();
counter = 0;
t=0;
do
{
t=t+1;
fscanf(stream,"%s\n",i);
fscanf(stream,"%s\n", person_list[t].initials);
fscanf(stream, "%s\n", person_list[t].surname);
fscanf(stream, "%s\n", person_list[t].name);
fscanf(stream, "%s\n", person_list[t].telno);
fscanf(stream, "\n");
counter=counter+1;
}while(!feof(stream));
fclose(stream);
return;
}