Sunday 20 January 2019

C database output to a neural network

The following is a template / code for a database which ouputs some of its datafields to a filename.dat file.  Such a file could be used to train data for a neural network such as FANN Tool. I have produced one column of four neural net inputs: phone, mail, email, visit for each person record. Another datafield could be sales  for a single neural net output.
The phone, mail, email and visit datafields store the number of responses from each person for the four approaches used
At the top of the .dat file would be three figures:
(i) How many records there are
(ii) How many inputs: four for one each of the values contained in phone, mail, email and visit.
(iii) How many outputs, for example, one for the number of sales for each person. For the output an additional datafield would need to be added to the database, such as number of sales or salary etc.

So if there are 1000 records, and four inputs and one output, the top of the data column would be

1000 4 1

and beneath that

Number of calls
Number of mails
Number of emails
Number of visit
Number of sales or salary etc

For each person / record.

So the first two records in the filename.dat file would be

1000 4 1
2
4
1
3
Number of sales or salary etc
3
2
1
4
Number of sales or salary etc


The code for the database can be copied and pasted into a text editor and saved as filename.c

From the directory containing filename.c, using a terminal, type
gcc filename.c

The program can be run by
./a.out

#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];
int phone;
int mail;
int Email;
int visit;
}addr_list[MAX];

struct addr temp;
int counter;
void init_list(void), enter(void);
void booklist(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
void swap(int u, int v);
void addresponse(void),  contactresponse(void);
void listresponses(void);
void searchclient(void);
void listtotalresponses(void), deleteresponses(void);
void saveresppipe(void);
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.  Response information for contact\n");
printf("9.  Output to a text file .txt for printing or import into wp\n");
printf("10. 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();
if(choice==7) search();
if(choice==8) contactresponse();
if(choice== 9) txtoutput();
if(choice== 10) exit(0);
}while(choice < 1 && choice >10);
}
return 0;
}

void init_list(void)
{
register int t;

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

void enter(void)
{
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';
addr_list[counter].phone='\0';
addr_list[counter].mail='\0';
addr_list[counter].Email='\0';
addr_list[counter].visit='\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(void)
{
register int u;
register int v;
for(u=1; u<counter; u++)
for (v=u+1; v<=counter; v++){
if( strcmp(addr_list[u].name, addr_list[v].name) >0){
swap(u, v);
}
}
}
void swap(int u, int v)
{
temp = addr_list[u];
addr_list[u]=addr_list[v];
addr_list[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<=counter; 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 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 such as filename.txt.\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");
fprintf(stream,"Phone calls %i\n",addr_list[t].phone);
fprintf(stream,"Mail %i\n",addr_list[t].mail);
fprintf(stream,"Emails %i\n",addr_list[t].Email);
fprintf(stream,"Visits %i\n",addr_list[t].visit);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}

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

if (strcmp(s, addr_list[i].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",addr_list[i].name);
printf("\nStreet: %s\n",addr_list[i].street);
printf("\nDistrict: %s\n",addr_list[i].district);
printf("\nTown: %s\n",addr_list[i].town);
printf("\nCounty: %s\n",addr_list[i].county);
printf("\nCountry: %s\n",addr_list[i].country);
printf("\nPostcode:%s\n",addr_list[i].postcode);
printf("\nTelephone number: %s\n",addr_list[i].telno);
printf("\nEmail address: %s\n",addr_list[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';
}
void contactresponse(void)
{
int choice;
char s[3];
do{
printf("\n");
printf("1.  List the records\n");
printf("2.  Add a response\n");
printf("3.  List the responses for a contact\n");
printf("4.  Delete the responses for a contact\n");
printf("5.  List total number of responses for all contacts\n");
printf("6.  Save responses to a filename.dat file for piping to Python\n");
printf("7.  Quit\n");
printf("\nEnter your choice: ");
readLine(s);   
choice=atoi(s);
if(choice==1) list();
if(choice== 2) searchclient();
if(choice==3) listresponses();
if(choice==4) deleteresponses();
if(choice==5) listtotalresponses();
if(choice==6) saveresppipe();
if(choice==7) break;
}while(choice>=1 && choice <= 6);
}

void listresponses()
{
int recno;
char s[40];
printf("\nEnter a record number.\n");
readLine(s);
recno=atoi(s);
printf("\nRecord number: %i\n", recno);
printf("\nName: %s\n",addr_list[recno].name);
printf("\nStreet: %s\n",addr_list[recno].street);
printf("\nDistrict: %s\n",addr_list[recno].district);
printf("\nTown: %s\n",addr_list[recno].town);
printf("\nCounty: %s\n",addr_list[recno].county);
printf("\nCountry: %s\n",addr_list[recno].country);
printf("\nPostcode:%s\n",addr_list[recno].postcode);
printf("\nTelephone number: %s\n",addr_list[recno].telno);
printf("\nEmail address: %s\n",addr_list[recno].email);
printf("\n");
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail:  %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
}

void searchclient(void)
{
int record;
int choice;
char s[40], t[40];
printf("\nEnter a record number.\n");
readLine(s);
record = atoi(s);
printf("\nRecord number: %i\n", record);
printf("\nName: %s\n",addr_list[record].name);
printf("\nStreet: %s\n",addr_list[record].street);
printf("\nDistrict: %s\n",addr_list[record].district);
printf("\nTown: %s\n",addr_list[record].town);
printf("\nCounty: %s\n",addr_list[record].county);
printf("\nCountry: %s\n",addr_list[record].country);
printf("\nPostcode:%s\n",addr_list[record].postcode);
printf("\nTelephone number: %s\n",addr_list[record].telno);
printf("\nEmail address: %s\n",addr_list[record].email);
printf("\n");
do
{
printf("1. Phone. ");
printf("2. Mail. ");
printf("3. Email. ");
printf("4. Visit. ");
printf("5. Exit. ");
printf("Enter choice ...");
readLine(t);   
choice=atoi(t);
if(choice== 1) addr_list[record].phone = addr_list[record].phone+1;
if(choice==2) addr_list[record].mail = addr_list[record].mail+1;
if(choice==3) addr_list[record].Email = addr_list[record].Email+1;
if(choice==4)addr_list[record].visit = addr_list[record].visit+1;
if(choice== 5) break;
}while(choice>=1 && choice <= 5);
}
void listtotalresponses()
{
    int k, totalphone, totalmail, totalEmail, totalvisits;
    totalphone=0;
    totalmail=0;
    totalEmail=0;
    totalvisits = 0;
    for(k=1; k<= counter; k++){
        totalphone=totalphone +addr_list[k].phone;
        totalmail = totalmail +addr_list[k].mail;
        totalEmail =totalEmail + addr_list[k].Email;
        totalvisits = totalvisits + addr_list[k].visit;
    }
    printf("Total number of phone calls: %i\n",totalphone);
    printf("Total number of mail: %i\n",totalmail);
    printf("Total number of Emails: %i\n",totalEmail);
    printf("Total number of visits: %i\n",totalvisits);
}

void deleteresponses()
{
int recno;
char s[40];
printf("\nEnter a record number.\n");
readLine(s);
recno=atoi(s);
printf("\nRecord number: %i\n", recno);
printf("\nName: %s\n",addr_list[recno].name);
printf("\nStreet: %s\n",addr_list[recno].street);
printf("\nDistrict: %s\n",addr_list[recno].district);
printf("\nTown: %s\n",addr_list[recno].town);
printf("\nCounty: %s\n",addr_list[recno].county);
printf("\nCountry: %s\n",addr_list[recno].country);
printf("\nPostcode:%s\n",addr_list[recno].postcode);
printf("\nTelephone number: %s\n",addr_list[recno].telno);
printf("\nEmail address: %s\n",addr_list[recno].email);
printf("\n");
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail:  %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
printf("\n");

addr_list[recno].phone='\0';
addr_list[recno].mail ='\0';
addr_list[recno].Email='\0';
addr_list[recno].visit='\0';   
printf("\nNew values for responses for record: %i\n",recno);
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail:  %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
printf("\n");
}

void saveresppipe()
{
int t;
int count;
FILE *stream;
char filename[30];
count = 0;
printf("Enter a file name such as filename.dat\n");
scanf("%s",filename);
printf("\n");
stream = fopen(filename, "w+");
fprintf(stream,"%i\n",counter);
for(t=1; t<MAX; t++) {
if(addr_list[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",addr_list[t].phone);
fprintf(stream,"%i\n",addr_list[t].mail);
fprintf(stream,"%i\n",addr_list[t].Email);
fprintf(stream,"%i\n",addr_list[t].visit);
}
}
fclose(stream);
return;
}