Tuesday 16 August 2011

C database to for organising your music collection.

This is a database which will run on Linux to organise your music collection. It can be compiled from a terminal using the GNU compiler. The command is gcc filename. This creates an output file called a.out. It can be renamed using the command mv a.out newfilename. The command to run the program is ./newfilename.


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

struct cd {
char artist[30];
char title[40];
char style[20];
}cd_list[MAX];

struct cd temp;
int counter;

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

int main(void)
{
char choice;

init_list();
for(;;) {
choice = menu_select();
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: copy();
break;
case 8: loadbackup();
break;
case 9: output();
break;
case 10: outputtwo();
break;
case 11: loadbackuptwo();
break;
case 12: search();
break;
case 13: searchbytitle();
break;
case 14: exit(0);
}
}
}

void init_list(void)
{
register int t;

for(t=1; t<MAX; ++t) cd_list[t].artist[0] ='\0';
}
int menu_select(void)
{
char s[80];
int c;

printf("\n");
printf("1.  Enter a CD\n");
printf("2.  Delete a CD\n");
printf("3.  List the file\n");
printf("4.  Save the file\n");
printf("5.  Load the file\n");
printf("6.  Sort the file\n");
printf("7.  Copy the file after saving\n");
printf("8.  Load the copied file\n");
printf("9.  Output to text file for printing or word processing\n");
printf("10. Output to a backup file with a name of your choice\n");
printf("11. Load from a backup file with a name of your choice\n");
printf("12. Search by artist\n");
printf("13. Search by title\n");
printf("14. Quit\n");
do {
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>14);
return c;
}

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

slot = find_free();

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

printf("Enter artist: ");
gets(cd_list[slot].artist);
printf("Enter title: ");
gets(cd_list[slot].title);
printf("Enter style, (A)mbient, (B)lues, (F)olk, (I)nstrumental, \n");
printf("(J)azz, (M)editation, (N)ew age, (R)ock, (S)oul, (T)heme: ");
gets(cd_list[slot].style);
counter = counter +1;
}

int find_free(void)
{
register int t;

for(t=1; cd_list[t].artist[0] && t<MAX; t++)

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

void delete(void)
{
int t;
int u;
printf("enter a record number #: ");
scanf("%i", &t);
for(u=t; u<=counter; u++){
strncpy(cd_list[u].artist, cd_list[u+1].artist, 30);
strncpy(cd_list[u].title, cd_list[u+1].title, 40);
strncpy(cd_list[u].style, cd_list[u+1].style, 20);
}
cd_list[counter].artist[0] = '\0';
cd_list[counter].title[0] = '\0';
cd_list[counter].style[0] = '\0';
counter = counter -1;

}

void list(void)
{
register int t;
int count;
count = 0;
for(t=1; t<MAX; t++) {
if(cd_list[t].artist[0]) {
count = count +1;
printf("CD number %i\n",count);
printf("%s\n", cd_list[t].artist);
printf("%s\n", cd_list[t].title);
printf("%s\n", cd_list[t].style);
printf("\n");
}
}
printf("\n\n");
}

void sort(void)
{
register int u;
register int t;
for (t=1; t<counter; t++){
for (u=t+1; u<=counter; u++)
if( strcmp(cd_list[t].artist, cd_list[u].artist) >0){
strncpy(temp.artist, cd_list[t].artist, 30);
strncpy(temp.title, cd_list[t].title, 40);
strncpy(temp.style, cd_list[t].style, 20);
strncpy(cd_list[t].artist, cd_list[u].artist, 30);
strncpy(cd_list[t].title, cd_list[u].title, 40);
strncpy(cd_list[t].style, cd_list[u].style, 20);
strncpy(cd_list[u].artist, temp.artist, 30);
strncpy(cd_list[u].title, temp.title, 40);
strncpy(cd_list[u].style, temp.style, 20);
}
}
}

void save(void)
{
FILE *fp;
register int i;

if((fp=fopen("cdlist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; i++)
if(*cd_list[i].artist)
if(fwrite(&cd_list[i],
sizeof(struct cd), 1, fp)!=1)
printf("File write error.\n");

fclose(fp);
}

void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("cdlist", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();

counter = 0;
for(i=1;  i<MAX; i++){
if(fread(&cd_list[i],
sizeof(struct cd), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter = counter +1;
}
fclose(fp);
}

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

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

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

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


void loadbackup(void)
{
FILE *fp;
register int i;

if((fp=fopen("cdlistbackup", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();
counter = 0;
for(i=1;  i<MAX; i++){
if(fread(&cd_list[i],
sizeof(struct cd), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter = counter +1;
}
fclose(fp);
}


void output(void)
{
int t;
int count;
FILE *stream;
count =0;
stream = fopen("cddboutput.txt", "w+");
for(t=1; t<MAX; t++) {
if(cd_list[t].artist[0]) {
count = count + 1;
fprintf(stream,"CD number %i\n",count);
fprintf(stream,"%s\n", cd_list[t].artist);
fprintf(stream,"%s\n", cd_list[t].title);
fprintf(stream,"%s\n", cd_list[t].style);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}

void outputtwo(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
gets(filename);
printf("\n");
if((fp=fopen(filename, "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; i++)
if(*cd_list[i].artist)
if(fwrite(&cd_list[i],
sizeof(struct cd), 1, fp)!=1)
printf("File write error.\n");

fclose(fp);
}


void loadbackuptwo(void)
{
register int i;
FILE *fp;
char filename[30];
printf("Enter a file name.\n");
gets(filename);
printf("\n");

if((fp=fopen(filename, "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();
counter = 0;
for(i=1;  i<MAX; i++){
if(fread(&cd_list[i],
sizeof(struct cd), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter = counter +1;
}
fclose(fp);
}


void search(void)
{
int i;
char s[30];
printf("\nEnter an artist.\n");
gets(s);
for(i=1;i<MAX;i++)
{
if (strcmp(s, cd_list[i].artist)==0)
{
printf("\nRecord number: %i\n",i);
printf("\nArtist: %s\n",cd_list[i].artist);
printf("\nTitle: %s\n",cd_list[i].title);
printf("\nStyle: %s\n",cd_list[i].style);
printf("\n");
}
}
}

void searchbytitle(void)
{
int i;
char s[30];
printf("\nEnter a title.\n");
gets(s);
for(i=1;i<MAX;i++)
{
if (strcmp(s, cd_list[i].title)==0)
{
printf("\nRecord number: %i\n",i);
printf("\nArtist: %s\n",cd_list[i].artist);
printf("\nTitle: %s\n",cd_list[i].title);
printf("\nStyle: %s\n",cd_list[i].style);
printf("\n");
}
}
}

No comments:

Post a Comment