Code:
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITLE 70
#define MAXACTOR 70
#define MAXVIDS 10
struct video {
char title[MAXTITLE];
char actor[MAXACTOR];
int rating;
};
typedef struct video vid;
int Size = sizeof(vid);
int addVideos(vid *vids); //function proto's
int idelete(vid *vids);
int edit(vid *vids);
int fetch(vid *vids, char *buff);
void menu(vid *vids, int vidNum);
void sortRecords(vid *vids);
void viewAll(vid *vids);
int writeAll(vid *vids, int vidNum);
int main(void)
{
vid vids[MAXVIDS];
char buff[80];
char ch;
int i;
int index, filecount, vidNum=0;
FILE * fp_in, *fp_out;
//gets rid of the junk from vids[] records
for (index = 0; index < MAXVIDS; index++) { // this is to
make all empty files equal to 'deleted' status
vids[index].title[0] = '\0'; // thus searchable
in an 'if' statement
strcpy(vids[index].actor, "");
vids[index].rating = 0;
}
if((fp_in = fopen("videodb.txt", "r")) == NULL) {
fputs("Can't open videodb.txt\n", stderr);
exit(EXIT_FAILURE);
}
i=0;
while((fgets(buff, sizeof(buff), fp_in))!=NULL) {
buff[strlen(buff)-1]='\0';
strcpy(vids[i].title, buff);
vidNum++;
fgets(buff, sizeof(buff), fp_in);
buff[strlen(buff)-1]='\0';
strcpy(vids[i].actor, buff);
fgets(buff, sizeof(buff), fp_in);
buff[strlen(buff)-1]='\0';
sscanf(buff, "%d", &vids[i].rating);
if(++i==MAXVIDS)
break;
}
fclose(fp_in);
menu(vids, vidNum);
return 0;
}
int addVideos(vid *vids) {
int i,j,ok;
char buff[80];
char ch='y';
vid vid1;
//for(i=0;i<4;i++) {
while(ch=='y' || ch=='Y') {
printf("\nEnter the vid's title: ");
fgets(buff, MAXTITLE, stdin);
buff[strlen(buff)-1]='\0';
strcpy(vid1.title, buff);
printf("Enter the vid's principal actor: ");
fgets(buff, MAXACTOR, stdin);
buff[strlen(buff)-1]='\0';
strcpy(vid1.actor, buff);
printf("\nEnter the value: ");
fgets(buff, sizeof(vids[0].rating), stdin);
sscanf(buff, "%d", &vid1.rating);
for(j=0,ok=0;j<MAXVIDS;j++) {
if(vids[j].title[0]=='\0') {
strcpy(vids[j].title, vid1.title);
strcpy(vids[j].actor, vid1.actor);
vids[j].rating = vid1.rating;
ok=1;
break;
}
}
if(!ok) {
printf("\nArray is full, couldn't add that record");
return 1; //add failed
}
printf("\nAdd another vid? [y/n]: ");
scanf("%1c", &ch);
(void) getchar();
}
return 0; //successful add
}
int edit(vid *vids) {
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(vids, buff); //in the list?
if(!i) //nope
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",
vids[i].title, vids[i].actor, vids[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(vids[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(vids[i].actor, buff);
}
}
printf("\n\t Enter new rating [0-6, or <enter> to leave it
unchanged]: ");
fgets(buff, sizeof(vids[0].rating), stdin);
if(strlen(buff) >1) {
sscanf(buff, "%d", &vids[i].rating);
}
sortRecords(vids);
return i;
}
int fetch(vid *vids, char *buff) {
int i=0, lo, hi, mid;
while(vids[i++].title[0]=='\0'); //find lo
lo=i-1;
hi = MAXVIDS;
while(lo <= hi) {
mid=(lo + hi)/2;
if(strcmp(vids[mid].title, buff) >0)
hi=mid-1;
else if(strcmp(vids[mid].title, buff) <0)
lo=mid+1;
else
return mid;
}
return 0;
}
int idelete(vid *vids) {
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';
i = fetch(vids, buff); //get buff's index in the array
if(i) {
printf("Delete this record [y/n] ? ");
printf("\n\t Title: %s \n\t Actor: %s \n\t Rating:
%d",vids[i].title,\
vids[i].actor, vids[i].rating);
fgets(buff, 2, stdin);
if(buff[0]=='y' || buff[0]=='Y') {
printf("\nHas been deleted");
getch();
getchar();
//*vids[i].title = "";
//*vids[i].actor = "";
//*vids[i].rating = 0;
}
return i;
}
else
return 0;
}
void menu(vid *vids, int vidNum) {
char ch;
char buff[80];
int i, n, index, ok=0;
FILE *fp;
do {
printf("\n <<<< Welcome to the Main
Menu >>>>");
printf("\n\n\t There are %d videos in the library \n",
vidNum);
printf("\n\t [a]dd a videos [d]elete a videos
\n");
printf("\n\t [e]dit a videos [f]etch a videos
\n");
printf("\n\t [v]iew all videoss [w]rite all videos
\n");
printf("\n\t\t Make a choice or Q to quit: ");
fgets(buff, sizeof(buff), stdin);
ch=buff[0];
switch (ch) {
case 'a':
printf("\nWould you like to add a new vid? [y/n]: ");
ch=getchar();
(void) getchar();
if(ch=='y' || ch=='Y') {
n=addVideos(vids);
if(n==0) { //a successful add
sortRecords(vids);
++vidNum;
}
}
break;
case 'd': ok=idelete(vids);
if(ok)
--vidNum;
break;
case 'e': edit(vids);
break;
case 'f':
printf("\nEnter the title of the vid: ");
fgets(buff, sizeof(vids[0]), stdin);
buff[strlen(buff)-1]='\0';
index = fetch(vids, buff);
if(index) {
printf("\n\t Title: %s\n\t Actor: %s\n\t Rating:
%2d",
vids[index].title, vids[index].actor,
vids[index].rating);
getch();
}
break;
case 'v': viewAll(vids); break;
case 'w': writeAll(vids, vidNum); break;
case 'Q': ch='q';
case 'q': break;
default: printf("\n\t\t Please enter a valid
selection\n");
};
}while(ch!='q');
}
void sortRecords(vid *vids ) {
int i, j, lo = 0, hi=MAXVIDS;
vid vid1;
char val[MAXTITLE];
j=hi;
for(i=lo+1;i<hi;i++) {
vid1 = vids[i];
j = i-1;
while((strcmp(vids[j].title, vid1.title)) > 0) {
vids[j + 1] = vids[j];
--j;
if(j<0) break;
}
vids[j+1] = vid1;
}
printf("\n In sorted order: \n");
for(i=0;i<10;i++) {
if(vids[i].title)
printf("\nTitle: %s Actor: %s Rating:
%2d",vids[i].title,vids[i].actor,vids[i].rating);
}
}
void viewAll(vid *vids) {
int i=0;
puts("\n\tHere is the list of your vids:\n");
while(vids[i].title[0]) {
//fwrite(&vids[i], Size, 1, stdout);
printf("\n Title: %s\n Actor: %s\n Ratings:
%2d",vids[i].title, vids[i].actor, vids[i].rating);
++i;
}
putchar('\n');
}
int writeAll(vid *vids, int vidNum) {
int i;
FILE *fp;
unlink("videos.bak");
if(rename("videodb.txt", "videodb.bak")==0) {
if((fp=fopen("videodb.txt", "wt"))==NULL) {
printf("\nError opening videos.txt file. No records were
written.\n");
return 1;
}
}else {
printf("\nError renaming in function writeAll()\n");
return 1;
}
i=0;
while(1) {
fprintf(fp,
"%s\n%s\n%d\n",vids[i].title,vids[i].actor,vids[i].rating);
++i;
if(i>vidNum) break;
}
fclose(fp);
return 0;
}