#include #include #include #include #include #define NUM_REC 100 #define NAME 100 #define AUTHOR 50 #define REF 20 typedef struct { char name[NAME]; //book name char author[AUTHOR]; //author char ref[REF]; //reference no. } BOOK; int showchoices(void); void listall(BOOK *, int); void showhead(void); void save(BOOK *, char *, int); int load(BOOK *, char *, int); void add(BOOK *, int *); int find(BOOK *, char *, int, char); void main(int argc, char *argv[]) { BOOK bk[NUM_REC]; int bc=0; char ch; char *filename; clrscr(); if(!argv[1]) { do { printf("\nEnter Choice: New session (N) or Update file (U) : "); ch=getche(); }while(ch!='u'&&ch!='U' && ch!='n'&&ch!='N'); if(ch=='u' || ch=='U') { printf("Enter FILENAME: "); scanf("%12s",filename); } } //end if else { ch='u'; filename=argv[1]; } if(ch=='n' || ch=='N') { printf("\nEnter FILENAME: "); scanf("%11s",filename); bc=load(bk, filename, 0); //filename not given as } // end if new file is to be made if(ch=='u' || ch=='U') { bc=load(bk, filename, 1); } int cha; showchoices: clrscr(); printf("File %s is open...\n",filename); cha=showchoices(); if(cha==0) //save &exit { printf("\nSave? [yn]"); if(cha=getch()=='y' || cha=='Y') { save(bk, filename, bc); } } else if(cha==1) //save & continue { save(bk, filename, bc); goto showchoices; } else if(cha==2) //find in DB { char jz; char *tofind; do{ printf("Enter the search pattern (in " "LOWERCASE please!)" " \nName or Author: "); jz=getche(); } while(jz!='a' && jz!='n'); printf("\nEnter the Text String to Find" "\n(Part strings OK): "); fflush(stdin); gets(tofind); cha=find(bk, tofind, bc, jz); if(cha==-1) puts("NOT FOUND"); else { printf("Required Record found at posn. %d\n",cha+1); puts("\nRecord is: "); printf("\nBook Name : %s",bk[cha].name); printf("\nAuthor : %s",bk[cha].author); printf("\nReference # %s",bk[cha].ref); } cha=getch(); goto showchoices; } else if(cha==3) //list all { listall(bk, bc); goto showchoices; } else if(cha==4) { add(bk, &bc); goto showchoices; } else { putch(7); goto showchoices; } } //----end main() int load(BOOK *b, char *fn, int fc) { int i=0; if(fc!=0) { FILE*stream=fopen(fn,"rt"); if(!stream) { puts("Error on input file"); exit(0); } while(!feof(stream)) { fscanf(stream," %[^\n]",(b+i)->name); fscanf(stream," %[^\n]",(b+i)->author); fscanf(stream," %[^\n]",(b+i)->ref); i++; } } //end the if(fn!="no file") if st.; NOT the fn() else { while(1) { add(b, &i); printf("Hit ESC to stop, or ENTER to add more records"); if(getch()==27) break; fflush(stdin); } } //end ELSE return i-1; } void save(BOOK *b,char *fn, int records) { char yn; do { printf("\nOld Filename is: %s. ",fn); printf("Enter NEW file name? [yn]"); yn=getche(); } while(yn!='Y' && yn!='y' && yn!='N' && yn!='n'); if(yn=='y' || yn=='Y') { printf("Enter FILENAME: "); gets(fn); } FILE*fp=fopen(fn,"wt"); for(int i=0;iname,fp); fputc('\n',fp); fputs((b+i)->author,fp); fputc('\n',fp); fputs((b+i)->ref,fp); fputc('\n',fp); } fclose(fp); } int showchoices(void) { printf("0: save and exit\n"); printf("1: save only\n"); printf("2: find in DB\n"); printf("3: list all\n"); printf("4: add to DB\n"); printf("Enter Choice: "); int ch; ch=getche(); ch-=48; return ch; } /* */ void showhead(void) { printf("BOOK NAME"); gotoxy(45,1); printf("AUTHOR"); gotoxy(65,1); printf("REF#"); } void listall(BOOK *b, int records) { int bn; for(bn=0;bnname); fflush(stdin); printf("Enter the name of AUTHOR: "); gets((b + *i)->author); fflush(stdin); printf("Enter the REFERENCE No. of book: "); gets((b + *i)->ref); *i+=1; } int find(BOOK *b, char *tofind, int max, char f) { int l=strlen(tofind); for(int i=0;iname[j]; else if(f=='a') *buff=(b+i)->author[j]; buff++; } *buff='\0'; buff-=l; if(!stricmp(tofind, buff)) return i; } return -1; }