Code:
/* Warning:
This is an unfinished program. Do not use it, until you have finished it.
Anyone using it does so at their own risk, which is substantial.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAXTITLE 77
#define MAXACTOR 78
#define MAXGENRA 50
#define MAXVIDS 1000
struct video {
char title[MAXTITLE];
char actor[MAXACTOR];
char genra[MAXGENRA];
int rating; //1-5 stars
};
typedef struct video vid;
vid videos[MAXVIDS];
int NumVideos;
//int Size = sizeof(vid);
int addVideos(void); //function proto's
int idelete(void);
int edit(void);
int fetch(char *buff);
void menu(void);
void sortRecords(void);
void view(void);
int writeAll(void);
int main(void) {
char buff[214];
int i;
FILE * fp_in;
if((fp_in = fopen("videodb.txt", "r")) == NULL) {
fputs("Error! Can't find or open videodb.txt\n", stderr);
return 1;
}
i=0;
while((fgets(buff, sizeof(buff), fp_in))!=NULL) {
buff[strlen(buff)-1]='\0';
//printf("Buff: %s\n\n",buff);getchar();
sscanf(buff, "%[^,], %[^,], %[^,], %d ", videos[i].title,videos[i].actor,videos[i].genra,&videos[i].rating);
//printf(" title: %s\n actors: %s\n genra: %s\n rating: %d\n\n",
// videos[i].title,videos[i].actor,videos[i].genra,videos[i].rating); getchar();
if(++i==MAXVIDS) {
printf("Error! The array to hold video data, is too small. Please enlarge it\n\n");
getchar();
break;
}
}
fclose(fp_in);
NumVideos=i;
sortRecords();
//view();
menu();
printf(" \n");
printf(" \r");
return 0;
}
// this function is 90% done
int addVideos(void) {
int i,j=0,ok=0,len;
char buff[80]={'\0'};
char ch='y';
vid vid1;
while(ch=='y' || ch=='Y') {
_gotoxy(1,11);
printf(" \n");
printf(" title: ");
_gotoxy(9,12);
fgets(buff, MAXTITLE, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(vid1.title, buff);
printf(" *\n");
printf(" principal actor(s): *");
_gotoxy(22,14);
fgets(buff, MAXACTOR, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(vid1.actor, buff);
printf(" *\n");
printf(" genra: *");
_gotoxy(9,16);
fgets(buff, MAXGENRA, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(vid1.genra, buff);
printf(" *\n");
printf(" rating [0-5]: * ");
_gotoxy(16,18);
fgets(buff, sizeof(videos[0].rating), stdin);
sscanf(buff, "%d", &vid1.rating);
if(NumVideos<MAXVIDS) {
videos[NumVideos]=vid1;
++NumVideos;
ok=1;
}else {
ok=0;
printf("\nArray is full, couldn't add that record");
break;
}
printf("\nAdd another video? [y/n]: ");
scanf(" %c", &ch);
getchar();
}
sortRecords();
writeAll();
//clean up screen
for(i=12;i<22;i+=2) {
_gotoxy(1,i);
printf(" *\n");
}
return ok; //success or failure variable
}
int edit(void) {
char buff[80];
int i=0;
printf("\nEnter the title of the video you want to edit: ");
fgets(buff, MAXTITLE, stdin);
if(buff[strlen(buff)-1]=='\n')
buff[strlen(buff)-1]='\0';
i = fetch(buff); //in the list?
if(i>-1)
//return 0;
//video was found, i is it's index in the array
printf("\n\t Title: %s\n\t Actor: %s\n\t rating: %2d\n",
videos[i].title, videos[i].actor, videos[i].rating);
printf("\n\t Enter new title [hit <enter> to leave it unchanged]: ");
fgets(buff, MAXTITLE, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]=='\n') {
buff[strlen(buff)-1]='\0';
strcpy(videos[i].title, buff);
}
}
printf("\n\t Enter new actor [<enter> to leave it unchanged]: ");
fgets(buff, MAXACTOR, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]=='\n') {
buff[strlen(buff)-1]='\0';
strcpy(videos[i].actor, buff);
}
}
printf("\n\t Enter new rating [0-6, or <enter> to leave it unchanged]: ");
fgets(buff, sizeof(videos[0].rating), stdin);
if(strlen(buff) >1) {
sscanf(buff, "%d", &videos[i].rating);
}
sortRecords();
return i;
}
int fetch(char *buff) {
int lo, hi, mid;
lo=0;
hi = NumVideos-1;
while(lo <= hi) {
mid=(lo + hi)/2;
if((strcmp(videos[mid].title, buff)) <0) {
lo=mid+1;
}
else if((strcmp(videos[mid].title, buff)) >0) {
hi=mid-1;
}
else
return mid;
}
return -1;
}
int idelete(void) {
char buff[80];
int i;
printf("\nEnter the title of the vid you want to delete: ");
fgets(buff, MAXTITLE, stdin);
buff[strlen(buff)-1]='\0';
printf("\n\n%s length: %d \n",buff,strlen(buff));
i = fetch(buff); //get buff's index in the array
if(i>-1) {
printf("Delete this record [y/n] ? ");
printf("\n\t Title: %s \n\t Actor: %s \n\t Rating: %d",videos[i].title,\
videos[i].actor, videos[i].rating);
fgets(buff, 2, stdin);
if(buff[0]=='y' || buff[0]=='Y') {
videos[i].title[0]='~';
videos[i].title[1]='\0';
printf("\nHas been deleted \n");
getchar();
//*vids[i].title = "";
//*vids[i].actor = "";
//*vids[i].rating = 0;
sortRecords();
--NumVideos;
writeAll();
}
return 1;
}else {
printf("That record was not found.\n");
}
return 0;
}
void menu(void) {
char ch;
char buff[80];
int i,n, index, ok=0;
do {
//clear the menu area of the display
_gotoxy(1,1);
//for(i=0;i<3;i++) {
//printf(" *\n");
//}
printf(" \n");
_gotoxy(1,2);
printf(" <<<< Welcome to the Main Menu >>>> \n");
printf(" \n");
printf(" [a] Add a video [d] Delete a video \n");
printf(" [e] Edit a video record [s] Search for a video \n");
printf(" [v] View all videos [w] Write out all videos \n");
printf(" *\n");
printf(" Your Choice, or Q to quit: \n");
printf(" *\n");
//i=_wherey();
_gotoxy(49,8); //row 8 is the Choice row in the menu
fgets(buff, sizeof(buff), stdin);
sscanf(&buff[0]," %c",&ch);
switch (ch) {
case 'a':
printf("\n Would you like to add a new video? [y/n]: ");
ch = getchar();
getchar();
if(ch=='y' || ch=='Y') {
n=addVideos();
if(n) { //a successful add
sortRecords();
_gotoxy(1,12);
printf(" One Video has been added. %d Videos in all now. **\n",NumVideos);
getchar();
}
}
_gotoxy(1,12);
printf(" *\n");
ch='\0';
break;
case 'd':
ok=idelete();
ch='\0';
break;
case 'e': edit(); //8888888888888888888888
break;
case 's':
for(i=0;i<10;i++) {
printf(" *\n");
}
_gotoxy(1,10);
printf("Enter the video's \n title: ");
fgets(buff, MAXTITLE, stdin); //sizeof(videos[0])
buff[strlen(buff)-1]='\0';
index = fetch(buff);
//printf("Index: %d\n",index); getchar();
if(index>-1) {
printf(" #%d) title: %s\n actors: %s\n genra: %s\n rating: %d\n\n",
index+1,videos[index].title,videos[index].actor,videos[index].genra,videos[index].rating);
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf(" *\n");
getchar();
}else {
printf("That video was not found. \n ");
}
ch='\0';
break;
case 'v': view(); ch='\0';break;
case 'w': writeAll(); break;
case 'Q': ch='q';
case 'q': break;
default: printf("\n\t\t Please enter a valid selection\n");
};
}while(ch!='q');
}
void sortRecords(void) {
int i, j;
vid temp;
for(i=1;i<NumVideos;i++) {
temp = videos[i];
j = i-1;
while((strcmp(videos[j].title, temp.title)) > 0) {
videos[j + 1] = videos[j];
--j;
if(j<0) break;
}
videos[j+1] = temp;
}
//printf("\n%s %s %s %d \n",videos[0].title, videos[0].actor, videos[0].genra, videos[0].rating);
//printf("%s %s %s %d \n",videos[241].title, videos[241].actor, videos[241].genra, videos[241].rating);
//getchar();
}
void view(void) {
int i=0,row=12;
_gotoxy(2,row-1);
printf(" \n");
while(i < NumVideos) {
_gotoxy(2,row);
printf(" \r");
printf("%3d) title: %-60s\n",i+1,videos[i].title);
printf(" actors: %-68s\n",videos[i].actor);
printf(" genra: %-60s\n",videos[i].genra);
printf(" rating: %-60d\n",videos[i].rating);
printf(" \r");
++i;
if(i%5==0 && i) {
row=7;
_getch();
}
row+=5;
}
for(i=0;i<4;i++) {
printf(" \n");
if(i==2)
printf("Last video record has been reached. Hit Enter to Continue. \n");
}
i=_wherey();
_gotoxy(60,i-2);
getchar();
//printf("\n\n");
}
int writeAll(void) {
int i;
FILE *fp;
if((fp=fopen("videodb.txt", "wt"))==NULL) {
printf("\nError opening videos.txt file. No records were written.\n");
return 1;
}
i=0;
while(i<NumVideos) {
if(videos[i].title[0]) {
fprintf(fp, "%s, %s, %s, %d\n",videos[i].title,videos[i].actor,videos[i].genra, videos[i].rating);
++i;
}
}
fclose(fp);
return 0;
}