Monday, 17 April 2017
A C database that uses a dynamic array.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
typedef struct addr_list{
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;
int counter;
struct addr_list **recordarray;
struct addr_list ***pointer = &recordarray;
void readLine(char buffer[]);
void enter(void), txtoutput(void), list(void);
void delete(void);
void load(void), save(void);
void swap(int u, int v), sort(void), search(void);
void endprog(void);
int main(void)
{
int choice;
char s[80];
counter = 0;
struct addr_list **recordarray = calloc(counter, sizeof(struct addr_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");
printf("\n");
do {
printf("\nEnter your choice: ");
readLine(s);
choice=atoi(s);
}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: txtoutput();
break;
case 9: endprog();
}
}
return 0;
}
void enter(void)
{
int i;
counter = counter + 1;
i =counter;
recordarray = realloc(* pointer, counter * sizeof(struct addr_list));
recordarray[i] = malloc(sizeof(addr_list));
if(!recordarray[i])
{
printf("Memory request failed\n");
return;
}
printf("Enter information on new customer\n");
printf("Enter name: ");
readLine(recordarray[i]->name);
printf("Enter street: ");
readLine(recordarray[i]->street);
printf("Enter district / parish: ");
readLine(recordarray[i]->district);
printf("Enter town / city: ");
readLine(recordarray[i]->town);
printf("Enter county: ");
readLine(recordarray[i]->county);
printf("Enter country: ");
readLine(recordarray[i]->country);
printf("Enter postcode: ");
readLine(recordarray[i]->postcode);
printf("Enter telephone number: ");
readLine(recordarray[i]->telno);
printf("Enter email address: ");
readLine(recordarray[i]->email);
printf("\nMemory at %i bytes after reading.\n", counter * sizeof(**recordarray));
}
void delete(void)
{
int t;
int u;
printf("\nenter a record number #: ");
scanf("%i", &t);
getchar();
for(u=t; u<=counter; u++){
recordarray[u] = recordarray[u+1];
}
counter = counter -1;
recordarray = realloc(* pointer, counter * sizeof(struct addr_list));
printf("\nRecord number %i deleted.\n",t);
printf("\nMemory at %i bytes after deletion.\n", counter * sizeof(**recordarray));
}
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", (*pointer)[t]->name);
printf("%s\n", (*pointer)[t]->street);
printf("%s\n", (*pointer)[t]->district);
printf("%s\n", (*pointer)[t]->town);
printf("%s\n", (*pointer)[t]->county);
printf("%s\n", (*pointer)[t]->country);
printf("%s\n", (*pointer)[t]->postcode);
printf("%s\n", (*pointer)[t]->telno);
printf("%s\n", (*pointer)[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(recordarray[u]->name, recordarray[v]->name) >0){
swap(u, v);
}
}
printf("\nRecords sorted.\n");
}
void swap(int u, int v)
{
struct addr_list *temp;
temp = recordarray[u];
recordarray[u] = recordarray[v];
recordarray[v] = temp;
}
void save(void)
{
FILE *file1;
FILE *fp;
register int i;
if((file1=fopen("filesize", "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
fprintf(file1,"%d\n",counter);
fclose(file1);
if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(i=1; i<=counter; i++)
if(recordarray[i]->name)
if(fwrite((*pointer)[i], sizeof(addr_list), 1, fp)!=1)
{
printf("File write error.\n");
fclose(fp);
return;
}
printf("\n%i records saved to file.\n", counter);
fclose(fp);
}
void load(void)
{
FILE *fp;
FILE *file1;
register int i;
if((file1=fopen("filesize", "r"))==NULL) {
printf("Cannot open file,\n");
return;
}
fscanf(file1,"%i", &counter);
fclose(file1);
if((fp=fopen("maillist", "rb"))==NULL) {
printf("Cannot open file.\n");
counter = 0;
return;
}
recordarray = realloc(* pointer, counter * sizeof(addr_list));
for(i=1; i<=counter; i++){
recordarray[i] = malloc(sizeof(addr_list));
if(!recordarray[i])
{
printf("Out of memory.\n");
return;
}
if(fread((*pointer)[i], sizeof(addr_list), 1, fp)!=1) {
printf("File read error.\n");
fclose(fp);
return;
}
}
printf("\n%i records loaded.\n",counter);
printf("\nMemory at %i bytes after reading.\n", counter * sizeof(**recordarray));
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<=counter; t++) {
if((*pointer)[t]->name[0]) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", (*pointer)[t]->name);
fprintf(stream,"%s\n", (*pointer)[t]->street);
fprintf(stream,"%s\n", (*pointer)[t]->district);
fprintf(stream,"%s\n", (*pointer)[t]->town);
fprintf(stream,"%s\n", (*pointer)[t]->county);
fprintf(stream,"%s\n", (*pointer)[t]->country);
fprintf(stream,"%s\n", (*pointer)[t]->postcode);
fprintf(stream,"%s\n", (*pointer)[t]->telno);
fprintf(stream,"%s\n", (*pointer)[t]->email);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}
void search(void)
{
int i;
int count;
int flag = 0;
char s[40];
count=0;
printf("\nEnter a name.\n");
readLine(s);
for(i=1;i<=counter;i++)
{
if(recordarray[i]->name[0]) {
count = count +1;
}
if (strcmp(s, recordarray[i]->name)==0)
{
flag = 1;
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",(*pointer)[i]->name);
printf("\nStreet: %s\n",(*pointer)[i]->street);
printf("\nDistrict: %s\n",(*pointer)[i]->district);
printf("\nTown: %s\n",(*pointer)[i]->town);
printf("\nCounty: %s\n",(*pointer)[i]->county);
printf("\nCountry: %s\n",(*pointer)[i]->country);
printf("\nPostcode:%s\n",(*pointer)[i]->postcode);
printf("\nTelephone number: %s\n",(*pointer)[i]->telno);
printf("\nEmail address: %s\n",(*pointer)[i]->email);
printf("\n");
}
}
if(flag == 0) printf("Record not found.\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 endprog(void)
{
free(*pointer);
exit(0);
}
Thursday, 2 March 2017
A C database that puts a date stamp on each record.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.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 year;
int month;
int day;
}addr_list[MAX];
struct addr temp;
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void txtoutput(void);
void search(void), searchbydate(void);
void readLine(char buffer[]);
void swap(int u, int v);
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 by name\n");
printf("8. Search by date\n");
printf("9. Output to a text file 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) searchbydate();
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)
{
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);
time_t lt;
lt = time(NULL);
struct tm tm = *localtime(<);
addr_list[i].year = tm.tm_year + 1900;
addr_list[i].month = tm.tm_mon + 1;
addr_list[i].day = tm.tm_mday;
printf("%i %i %i\n", addr_list[i].day, addr_list[i].month,
addr_list[i].year);
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].year = '\0';
addr_list[counter].month = '\0';
addr_list[counter].day = '\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("%i %i %i\n",addr_list[t].day, addr_list[t].month,
addr_list[t].year);
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<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 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,"%i %i %i\n", addr_list[t].day,
addr_list[t].month, addr_list[t].year);
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);
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("\nDate: %i %i %i\n", addr_list[i].day,
addr_list[i].month, addr_list[i].year);
printf("\n");
}
}
}
void searchbydate(void)
{
int i;
int count;
count=0;
int d, m, y;
printf("\nEnter a day of the month for example 10\n");
scanf("%i",&d);
printf("\nEnter a month of the year for example 2\n");
scanf("%i",&m);
printf("\nEnter a year for example 2017\n");
scanf("%i",&y);
getchar();
for(i=1;i<MAX;i++)
{
if(addr_list[i].name[0]) {
count = count +1;
}
if(addr_list[i].day== d && addr_list[i].month==m
&& addr_list[i].year==y)
{
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("\nDate record created: %i %i %i\n",addr_list[i].day,
addr_list[i].month, addr_list[i].year);
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';
}
#include<stdlib.h>
#include<string.h>
#include<time.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 year;
int month;
int day;
}addr_list[MAX];
struct addr temp;
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void txtoutput(void);
void search(void), searchbydate(void);
void readLine(char buffer[]);
void swap(int u, int v);
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 by name\n");
printf("8. Search by date\n");
printf("9. Output to a text file 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) searchbydate();
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)
{
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);
time_t lt;
lt = time(NULL);
struct tm tm = *localtime(<);
addr_list[i].year = tm.tm_year + 1900;
addr_list[i].month = tm.tm_mon + 1;
addr_list[i].day = tm.tm_mday;
printf("%i %i %i\n", addr_list[i].day, addr_list[i].month,
addr_list[i].year);
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].year = '\0';
addr_list[counter].month = '\0';
addr_list[counter].day = '\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("%i %i %i\n",addr_list[t].day, addr_list[t].month,
addr_list[t].year);
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<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 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,"%i %i %i\n", addr_list[t].day,
addr_list[t].month, addr_list[t].year);
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);
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("\nDate: %i %i %i\n", addr_list[i].day,
addr_list[i].month, addr_list[i].year);
printf("\n");
}
}
}
void searchbydate(void)
{
int i;
int count;
count=0;
int d, m, y;
printf("\nEnter a day of the month for example 10\n");
scanf("%i",&d);
printf("\nEnter a month of the year for example 2\n");
scanf("%i",&m);
printf("\nEnter a year for example 2017\n");
scanf("%i",&y);
getchar();
for(i=1;i<MAX;i++)
{
if(addr_list[i].name[0]) {
count = count +1;
}
if(addr_list[i].day== d && addr_list[i].month==m
&& addr_list[i].year==y)
{
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("\nDate record created: %i %i %i\n",addr_list[i].day,
addr_list[i].month, addr_list[i].year);
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';
}
A C database that uses the C qsort routine and a binary chop search.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
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];
}addr;
addr addr_list[MAX];
int cmp (const void * a, const void * b)
{
const addr *addrA = a;
const addr *addrB = b;
if(strcmp(addrA->name, addrB->name)>0)
return 1;
if(strcmp(addrA->name, addrB->name)<0)
return -1;
if(strcmp(addrA->name, addrB->name)==0)
return 0;
}
int num_addresses;
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
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) qsort(addr_list, counter+1, sizeof(addr), &cmp);
/*I assume it is counter+1 for qsort instead of counter because array of records
starts at addr_list[1] instead of addr_list[0]*/
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 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(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(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(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(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';
}
#include<stdlib.h>
#include<string.h>
#define MAX 10000
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];
}addr;
addr addr_list[MAX];
int cmp (const void * a, const void * b)
{
const addr *addrA = a;
const addr *addrB = b;
if(strcmp(addrA->name, addrB->name)>0)
return 1;
if(strcmp(addrA->name, addrB->name)<0)
return -1;
if(strcmp(addrA->name, addrB->name)==0)
return 0;
}
int num_addresses;
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
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) qsort(addr_list, counter+1, sizeof(addr), &cmp);
/*I assume it is counter+1 for qsort instead of counter because array of records
starts at addr_list[1] instead of addr_list[0]*/
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 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(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(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(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(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';
}
Thursday, 1 December 2016
A simple countdown timer written in C using gcc.
#include <stdio.h>
int hcount;
int mcount;
int scount;
int main(void)
{
printf("input the hours ");
scanf("%i", &hcount);
printf("input the minutes ");
scanf("%i", &mcount);
printf("input the seconds ");
scanf("%i", &scount);
for(;;)
{
sleep(1);
scount--;
if(scount<0) {
scount =59;
mcount--;
}
if(mcount<0){
mcount=59;
hcount--;
}
if(hcount==0 && mcount==0 && scount==0){
break;
}
printf("\n%i\n", scount);
printf("\n%i\n", mcount);
printf("\n%i\n", hcount);
}
return 0;
}
Thursday, 28 July 2016
A database in C that uses error trapping in the create new record and delete record routines to make it user friendly.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
typedef struct {
char name[42];
char street[42];
char district[42];
char town[42];
char county[42];
char country[42];
char postcode[42];
char telno[42];
char email[42];
}addr_list;
addr_list address[MAX];
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), copyfile(void), sort(void);
void loadbackup(void), outputthree(void);
void outputtwo(void), search(void);
void readLine(char buffer[]);
void swap(int u, int v);
void readinteger(char intbuffer[]);
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 or copy\n");
printf("10. Copy master file\n");
printf("11. Output to a text file for printing or import into wp\n");
printf("12. Quit\n");
do {
printf("\nEnter your choice: ");
readLine(s);
choice=atoi(s);
}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: copyfile();
break;
case 11: outputthree();
break;
case 12: 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");
do
{
printf("Enter name: ");
readLine(address[i].name);
if(strlen(address[i].name)>40)
{
printf("String length is %i\n",strlen(address[i].name));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].name)>40);
do
{
printf("Enter street: ");
readLine(address[i].street);
if(strlen(address[i].street)>40)
{
printf("String length is %i\n",strlen(address[i].street));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].street)>40);
do
{
printf("Enter district / parish: ");
readLine(address[i].district);
if(strlen(address[i].district)>40)
{
printf("String length is %i\n",strlen(address[i].district));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].district)>40);
do
{
printf("Enter town / city: ");
readLine(address[i].town);
if(strlen(address[i].town)>40)
{
printf("String length is %i\n",strlen(address[i].town));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].town)>40);
do
{
printf("Enter county: ");
readLine(address[i].county);
if(strlen(address[i].county)>40)
{
printf("String length is %i\n",strlen(address[i].county));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].county)>40);
do
{
printf("Enter country: ");
readLine(address[i].country);
if(strlen(address[i].country)>40)
{
printf("String length is %i\n",strlen(address[i].country));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].country)>40);
do
{
printf("Enter postcode: ");
readLine(address[i].postcode);
if(strlen(address[i].postcode)>40)
{
printf("String length is %i\n",strlen(address[i].postcode));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].postcode)>40);
do
{
printf("Enter telephone number: ");
readLine(address[i].telno);
if(strlen(address[i].telno)>40)
{
printf("String length is %i\n",strlen(address[i].telno));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].telno)>40);
do
{
printf("Enter email address: ");
readLine(address[i].email);
if(strlen(address[i].email)>40)
{
printf("String length is %i\n",strlen(address[i].email));
printf("Please restrict to forty characters.\n");
}
}while(strlen(address[i].email)>40);
counter=counter+1;
}
void delete(void)
{
char number[20];
int t;
int u;
int count;
int flag=0;
printf("enter a record number #: ");
readLine(number);
for(count=0;number[count]!='\0';count++)
{
if(!isdigit(number[count]))
{
printf("Not a valid number\n.");
flag = 1;
break;
}
}
if(flag == 0)
{
if(number[0] == '\0')
printf("No number entered.\n");
else
{
t=atoi(number);
if(t<1 || t >counter)
printf("Record number not within range.\n");
if(t>=1 && t <=counter)
{
for(u=t; u<=counter; u++){
address[u] = address[u+1];
}
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;
printf("Record deleted.\n");
}
}
}
}
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);
if(strlen(s)!=0)
{
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 copyfile(void)
{
FILE *in, *out;
int c;
char filename[30];
if ((in = fopen ("maillist", "r")) ==NULL){
printf("Cant open %s file for reading.\n","cdlist");
}
printf("Enter a file name.for the target file\n");
scanf("%s",filename);
printf("\n");
if ((out = fopen(filename, "w")) ==NULL){
printf("Cant open %s for writing.\n", "cdlistbackup");
}
while ((c = getc (in)) !=EOF)
putc (c, out);
fclose (in);
fclose(out);
printf("File has been copied.\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 readinteger(char intbuffer[])
{
int number;
int j=0;
do
{
number=getchar();
intbuffer[j]=number;
++j;
}
while(number !='\n');
intbuffer[j-1]='\0';
}
C program for regression / line of best fit for when there is scatter of data. the equation is y = a + bx
#include<stdio.h>
main()
{
int i, noofvar;
float xi, yi, xsum, ysum, xysum, xxsum, a, b;
xsum = 0;
ysum = 0;
xysum = 0;
xxsum = 0;
printf("The output of this program will give two values a and b.\n");
printf("a and b can be used in the equation y = a + bx.\n");
printf("Using the equation with a and b will give a regression.\n");
printf("A plot from the equation will give a line of best fit.\n");
printf("Enter the number of pairs of x and y variables:\n");
scanf("%i", &noofvar);
for(i = 1; i<=noofvar; i++)
{
printf("Enter x value %i\n",i);
scanf("%f", &xi);
printf("Enter y value %i\n", i);
scanf("%f", &yi);
xsum = xsum + xi;
ysum = ysum + yi;
xysum = xysum + xi * yi;
xxsum = xxsum + xi * xi;
}
b = (noofvar * xysum - xsum * ysum) / (noofvar * xxsum - xsum * xsum);
a = (ysum - b * xsum) / noofvar;
printf("Value of a is %f\n", a);
printf("Value of b is %f\n", b);
}
main()
{
int i, noofvar;
float xi, yi, xsum, ysum, xysum, xxsum, a, b;
xsum = 0;
ysum = 0;
xysum = 0;
xxsum = 0;
printf("The output of this program will give two values a and b.\n");
printf("a and b can be used in the equation y = a + bx.\n");
printf("Using the equation with a and b will give a regression.\n");
printf("A plot from the equation will give a line of best fit.\n");
printf("Enter the number of pairs of x and y variables:\n");
scanf("%i", &noofvar);
for(i = 1; i<=noofvar; i++)
{
printf("Enter x value %i\n",i);
scanf("%f", &xi);
printf("Enter y value %i\n", i);
scanf("%f", &yi);
xsum = xsum + xi;
ysum = ysum + yi;
xysum = xysum + xi * yi;
xxsum = xxsum + xi * xi;
}
b = (noofvar * xysum - xsum * ysum) / (noofvar * xxsum - xsum * xsum);
a = (ysum - b * xsum) / noofvar;
printf("Value of a is %f\n", a);
printf("Value of b is %f\n", b);
}
Tuesday, 31 May 2016
New ebook on simple C listings for indexed sequential file databases
I have just published a new ebook on simple C listings for indexed sequential file databases. The book and the C source code / C listings contained in it are open source. The link to the website to download it for free is
Open source ebook on databases written in C.
A website that has free Windows programs to download including working examples of C programs has the following link
A website of free programs including C databases.
Open source ebook on databases written in C.
A website that has free Windows programs to download including working examples of C programs has the following link
A website of free programs including C databases.
Monday, 23 May 2016
A barchart plotter for integers written in C. Data saved to file and barchart written to txt file for import into word processor
This program is compiled in Linux with the gcc command
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.
From the terminal using the directory containg the file / compiler output the program can b run using ./a.out
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define max 100
int number, ymax;
int ploty[max];
void createbar(void), updatebar(void), savedata(void), loadbar(void), plot(void), savebartxt(void);
int main(void)
{
int choice;
for(;;) {
printf("\n");
printf("1. Create a barchart.\n");
printf("2. Save barchart to a .txt file for printing.\n");
printf("3. Save data to to a data file to plot again later\n");
printf("4. Append data to a barchart.\n");
printf("5. Load a bar chart from data file.\n");
printf("6. Quit\n");
do {
printf("\nEnter your choice: ");
scanf("%i", &choice);
}while(choice < 0 && choice > 5);
switch(choice) {
case 1: createbar();
break;
case 2: savebartxt();
break;
case 3: savedata();
break;
case 4: updatebar();
break;
case 5: loadbar();
break;
case 6: exit(0);
}
}
return 0;
}
void createbar(void)
{
int y_data;
int i, j, k;
for(k=1; k<=max; k++)
ploty[k] = '\0';
printf("Enter the number of y points to plot.\n");
printf("Maximum number of points is 100 or type 111 to exit.\n");
scanf("%i", &number);
if(number == 111) exit(0);
printf("Enter the maximum / largest yvalue.\n");
scanf("%i", &ymax);
for(j=1; j<=number; j++)
{
printf("Enter y datum %i\n", j);
scanf("%i", &y_data);
ploty[j]=y_data;
}
plot();
}
void plot(void)
{
int xdatum, ydatum;
int i;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf( "y_data---------->\n");
for(i=1; i<=number; i++)
{
yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=yvalue; ydatum++)
printf("*");
printf("\n");
}
printf("\n");
}
}
void updatebar(void)
{
int xdatum, ydatum;
int y_data;
int i, j, l;
FILE *prnt;
for(l=1; l<=max; l++)
ploty[l] = '\0';
printf("Enter the number of y points to plot.\n");
scanf("%i", &number);
for(j=1; j<=number; j++)
{
printf("Enter y datum %i\n", j);
scanf("%i", &y_data);
ploty[j]=y_data;
}
printf( "y_data---------->\n");
for(i=1; i<=number; i++)
{
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=ploty[i]; ydatum++)
printf("*");
printf("\n");
}
printf("\n");
}
FILE *fp;
char filename[30];
printf("Enter a file name .txt to add bars to an already existing chart.\n");
scanf("%s",filename);
printf("\n");
fp = fopen(filename, "a+");
if((fp=fopen(filename, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}
time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(<);
fprintf(fp,"%s\n",asctime(ptr));
for(i=1; i<=number; i++)
{
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=ploty[i]; ydatum++)
fprintf(fp,"*");
fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);
FILE *fp2;
char filename2[30];
int k;
printf("Enter a file name .dat to add data to plot at a later date.\n");
scanf("%s",filename2);
printf("\n");
fp2 = fopen(filename2, "a+");
if((fp2=fopen(filename2, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(k = 1; k<= number; k++)
{
fprintf(fp2,"%i", ploty[k]);
fprintf(fp2,"\n");
}
fclose(fp2);
return;
}
void savedata(void)
{
FILE *fp;
char filename[30];
int i;
printf("Enter a file name .dat to contain data to plot at a later.\n");
printf("This can be printed using a text editor.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
fprintf(fp,"%i\n", ymax);
for(i = 1; i<= number; i++)
{
fprintf(fp,"%i", ploty[i]);
fprintf(fp,"\n");
}
fclose(fp);
return;
}
void loadbar(void)
{
int i;
char filename[30];
FILE *fp;
number = 0;
for(i=1; i<=max; i++)
ploty[i] ='\0';
printf("Enter a file name .dat containing data to plot.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "r"))==NULL) {
printf("Cannot open file.\n");
return;
}
fscanf(fp,"%i", &ymax);
while(!feof(fp))
{
number = number + 1;
fscanf(fp, "%i", &ploty[number]);
}
fclose(fp);
plot();
}
void savebartxt(void)
{
FILE *fp;
char filename[30];
int i;
int xdatum, ydatum;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf("Enter a file .txt name to contain the bar chart.\n");
printf("This can be printed using a text editor. \n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(<);
fprintf(fp,"%s\n",asctime(ptr));
fprintf(fp, "y_data---------->\n");
for(i=1; i<=number; i++)
{
yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=yvalue; ydatum++)
fprintf(fp,"*");
fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);
return;
}
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.
From the terminal using the directory containg the file / compiler output the program can b run using ./a.out
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define max 100
int number, ymax;
int ploty[max];
void createbar(void), updatebar(void), savedata(void), loadbar(void), plot(void), savebartxt(void);
int main(void)
{
int choice;
for(;;) {
printf("\n");
printf("1. Create a barchart.\n");
printf("2. Save barchart to a .txt file for printing.\n");
printf("3. Save data to to a data file to plot again later\n");
printf("4. Append data to a barchart.\n");
printf("5. Load a bar chart from data file.\n");
printf("6. Quit\n");
do {
printf("\nEnter your choice: ");
scanf("%i", &choice);
}while(choice < 0 && choice > 5);
switch(choice) {
case 1: createbar();
break;
case 2: savebartxt();
break;
case 3: savedata();
break;
case 4: updatebar();
break;
case 5: loadbar();
break;
case 6: exit(0);
}
}
return 0;
}
void createbar(void)
{
int y_data;
int i, j, k;
for(k=1; k<=max; k++)
ploty[k] = '\0';
printf("Enter the number of y points to plot.\n");
printf("Maximum number of points is 100 or type 111 to exit.\n");
scanf("%i", &number);
if(number == 111) exit(0);
printf("Enter the maximum / largest yvalue.\n");
scanf("%i", &ymax);
for(j=1; j<=number; j++)
{
printf("Enter y datum %i\n", j);
scanf("%i", &y_data);
ploty[j]=y_data;
}
plot();
}
void plot(void)
{
int xdatum, ydatum;
int i;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf( "y_data---------->\n");
for(i=1; i<=number; i++)
{
yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=yvalue; ydatum++)
printf("*");
printf("\n");
}
printf("\n");
}
}
void updatebar(void)
{
int xdatum, ydatum;
int y_data;
int i, j, l;
FILE *prnt;
for(l=1; l<=max; l++)
ploty[l] = '\0';
printf("Enter the number of y points to plot.\n");
scanf("%i", &number);
for(j=1; j<=number; j++)
{
printf("Enter y datum %i\n", j);
scanf("%i", &y_data);
ploty[j]=y_data;
}
printf( "y_data---------->\n");
for(i=1; i<=number; i++)
{
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=ploty[i]; ydatum++)
printf("*");
printf("\n");
}
printf("\n");
}
FILE *fp;
char filename[30];
printf("Enter a file name .txt to add bars to an already existing chart.\n");
scanf("%s",filename);
printf("\n");
fp = fopen(filename, "a+");
if((fp=fopen(filename, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}
time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(<);
fprintf(fp,"%s\n",asctime(ptr));
for(i=1; i<=number; i++)
{
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=ploty[i]; ydatum++)
fprintf(fp,"*");
fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);
FILE *fp2;
char filename2[30];
int k;
printf("Enter a file name .dat to add data to plot at a later date.\n");
scanf("%s",filename2);
printf("\n");
fp2 = fopen(filename2, "a+");
if((fp2=fopen(filename2, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(k = 1; k<= number; k++)
{
fprintf(fp2,"%i", ploty[k]);
fprintf(fp2,"\n");
}
fclose(fp2);
return;
}
void savedata(void)
{
FILE *fp;
char filename[30];
int i;
printf("Enter a file name .dat to contain data to plot at a later.\n");
printf("This can be printed using a text editor.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
fprintf(fp,"%i\n", ymax);
for(i = 1; i<= number; i++)
{
fprintf(fp,"%i", ploty[i]);
fprintf(fp,"\n");
}
fclose(fp);
return;
}
void loadbar(void)
{
int i;
char filename[30];
FILE *fp;
number = 0;
for(i=1; i<=max; i++)
ploty[i] ='\0';
printf("Enter a file name .dat containing data to plot.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "r"))==NULL) {
printf("Cannot open file.\n");
return;
}
fscanf(fp,"%i", &ymax);
while(!feof(fp))
{
number = number + 1;
fscanf(fp, "%i", &ploty[number]);
}
fclose(fp);
plot();
}
void savebartxt(void)
{
FILE *fp;
char filename[30];
int i;
int xdatum, ydatum;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf("Enter a file .txt name to contain the bar chart.\n");
printf("This can be printed using a text editor. \n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(<);
fprintf(fp,"%s\n",asctime(ptr));
fprintf(fp, "y_data---------->\n");
for(i=1; i<=number; i++)
{
yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=yvalue; ydatum++)
fprintf(fp,"*");
fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);
return;
}
f = cos(x) + cos(y) implemented in C
This program is compiled in Linux using the gcc command
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.
#include<stdio.h>
#include<math.h>
main()
{
const space = ' ', screenheight = 60, ycentre = 30, screenwidth = 70;
const xcentre = 35;
float xscale =7.0 , yscale = 7.0;
char screen[screenwidth][screenheight];
float xs, ys, interval, fvalue;
int period, row, col, ft, x, y, startx, starty, stopx, stopy;
char point;
/*insert spaces*/
for(col=0; col<=screenwidth; col++)
for(row=0; row<=screenheight; row++)
screen[row][col] = space;
startx = -xcentre;
stopx = xcentre;
starty = -ycentre;
stopy = ycentre;
for(y = starty; y <= stopy; y++)
for(x = startx; x <= stopx; x++)
{
xs = x/xscale;
ys = y/yscale;
printf("xs =%f\n",xs);
printf("xy=%f\n",ys);
fvalue = cos(xs) + cos(ys);
ft=round((fvalue + 2) * 2.5);
printf("%i\n", ft);
switch(ft)
{
case 0: point = 'A';
break;
case 1: point = ' ';
break;
case 2: point = 'B';
break;
case 3: point = ' ';
break;
case 4: point = 'C';
break;
case 5: point = ' ';
break;
case 6: point = 'D';
break;
case 7: point = ' ';
break;
case 8: point = 'E';
break;
case 9: point = ' ';
break;
case 10: point = 'F';
}
screen[x+xcentre][y+ycentre] = point;
}
/*draw graph*/
for(row =0; row <= screenheight; row++)
{
for(col=0; col<=screenwidth; col++)
printf("%c",screen[row][col]);
printf("\n");
}
}
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.
#include<stdio.h>
#include<math.h>
main()
{
const space = ' ', screenheight = 60, ycentre = 30, screenwidth = 70;
const xcentre = 35;
float xscale =7.0 , yscale = 7.0;
char screen[screenwidth][screenheight];
float xs, ys, interval, fvalue;
int period, row, col, ft, x, y, startx, starty, stopx, stopy;
char point;
/*insert spaces*/
for(col=0; col<=screenwidth; col++)
for(row=0; row<=screenheight; row++)
screen[row][col] = space;
startx = -xcentre;
stopx = xcentre;
starty = -ycentre;
stopy = ycentre;
for(y = starty; y <= stopy; y++)
for(x = startx; x <= stopx; x++)
{
xs = x/xscale;
ys = y/yscale;
printf("xs =%f\n",xs);
printf("xy=%f\n",ys);
fvalue = cos(xs) + cos(ys);
ft=round((fvalue + 2) * 2.5);
printf("%i\n", ft);
switch(ft)
{
case 0: point = 'A';
break;
case 1: point = ' ';
break;
case 2: point = 'B';
break;
case 3: point = ' ';
break;
case 4: point = 'C';
break;
case 5: point = ' ';
break;
case 6: point = 'D';
break;
case 7: point = ' ';
break;
case 8: point = 'E';
break;
case 9: point = ' ';
break;
case 10: point = 'F';
}
screen[x+xcentre][y+ycentre] = point;
}
/*draw graph*/
for(row =0; row <= screenheight; row++)
{
for(col=0; col<=screenwidth; col++)
printf("%c",screen[row][col]);
printf("\n");
}
}
Sine wave plot implemented in C
This program is compiled using the gcc command in Linux
gcc programname.c -lm
The -lm means the mathe functions are included during compilation.
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
main()
{
const increment = 15, scale = 30, centre = 40;
int angle;
int i, j;
angle = 0;
do
{
j = centre-scale*sin(angle*PI/180);
for(i=1; i<=j; i++)
printf(" ");
printf("sine\n");
angle = angle + increment;
}while(angle <= 720);
return 0;
}
gcc programname.c -lm
The -lm means the mathe functions are included during compilation.
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
main()
{
const increment = 15, scale = 30, centre = 40;
int angle;
int i, j;
angle = 0;
do
{
j = centre-scale*sin(angle*PI/180);
for(i=1; i<=j; i++)
printf(" ");
printf("sine\n");
angle = angle + increment;
}while(angle <= 720);
return 0;
}
Tuesday, 28 October 2014
A database that uses a random access file written in C
#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);
}
#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);
}
A sorted single linked list database written 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;
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;
}
}
#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;
}
}
Wednesday, 25 June 2014
A database in C using a single 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;
}
}
#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 that implements 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';
}
#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 databases 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';
}
Subscribe to:
Posts (Atom)