#include<stdio.h>
#include<conio.h>
#include<string.h>
struct libraryInfo
{char code[10];
char title[50];
char author[20];
int no_copies;
};
int enterOption(void);
void bookProfile(FILE*);
void transaction(FILE*);
void report(FILE*);
int enterChoice(void);
void addRecord(FILE*);
void editRecord(FILE*);
void deleteRecord(FILE*);
void borrowedBook(FILE*);
void returnedBook(FILE*);
int enterPick(void);
int main()
{FILE * cfPtr;
int option;
if((cfPtr=fopen("library.info","r+"))==NULL)
printf("File could not be opened.\n");
else{
while((option=enterOption())!=5)
{switch(option)
{case1:
bookProfile(cfPtr);
break;
case2:
transaction(cfPtr);
break;
case3:
report(cfPtr);
break;
case4:
printf("End of Run\n");
break;
}
}
fclose(cfPtr);
}
return 0;
}
void bookProfile()
{FILE * cfPtr;
int choice;
if((cfPtr=fopen("library.info","r+"))==NULL)
printf("File could not be opened.\n");
else{
while((choice=enterChoice())!=5)
{
switch(choice)
{case1:
addRecord(cfPtr);
break;
case2:
editRecord(cfPtr);
break;
case3:
deleteRecord(cfPtr);
break;
case4:
main();
break;
}
}
}
void transaction()
{FILE * cfPtr;
int pick;
if((cfPtr=fopen("library.info","r+"))=NULL)
printf("File could not be opened.\n");
else{
while((pick=enterPick())!=4)
{
switch(pick)
{case1:
borrowedBook(cfPtr);
break;
case2:
returnedBook(cfPtr);
break;
case3:
main();
break;
}
}
fclose(cfPtr);
}
return 0;
}
void record()
{FILE * cfPtr;
if((cfPtr=fopen("library.info","r"))==NULL)
printf("File could not be opened.\n");
else{
printf("%s%s%s%d","Code","Title","Author","No. of Copies");
fscanf(cfPtr,"%s%s%s%d",code,title,author,no_copies);
while(!feof(cfPtr)){
printf("%s%s%s%d\n",code,title,author,no_copies);
fscanf(cfPtr,"%s%s%s%d",code,title,author,no_copies);
}
fclose(cfPtr);
}
return 0;
}
void addRecord()
{FILE * cfPtr;
int choice;
if((cfPtr=fopen("library.info","r+"))==NULL)
printf("File could not be opened.\n");
else{
while((choice=enterChoice())!=3)
{
switch(choice)
{case1:
add(cfPtr);
break;
case2:
bookProfile(cfPtr);
break;
}
}
fclose(cfPtr);
}
void editRecord()
{FILE * cfPtr;
int choice;
if((cfPtr=fopen("library.info","r+"))==NULL)
printf("File could not be opened.\n");
else{
while((choice=enterChoice())!=3)
{
switch(choice)
{case1:
edit(cfPtr);
break;
case2:
bookProfile(cfPtr);
break;
}
}
fclose(cfPtr);
}
void deleteRecord()
{int choice;
FILE*cfPtr;
if((cfPtr=fopen("library.info","r+"))==NULL)
printf("File could not be opened.\n");
else{
while((choice=enterChoice())!=3)
{
switch(choice)
{case1:
delete(cfPtr);
break;
case2:
bookProfile(cfPtr);
break;
}
}
fclose(cfPtr);
}
void add()
{FILE * cfPtr;
char ans;
cfPtr=fopen("libraryInfo","w");
do
{printf("Enter book code:");
scanf("%s",&book.code);
printf("Enter book title:");
scanf("%s",&book.title);
printf("Do you want to add entry?\n");
ans=getch();
fprintf(cfPtr,"%s\t%s\n",book.code,book.title);
}while(to upper(ans=='Y');
printf("Book Code\t\t Book Title\n");
fclose(cfPtr);
cfPtr=fopen("library.info","r");
while(fscanf(cfPtr,"%s%s",&book.code,&book.title)!=EOF)
printf("%s\t\t%s",book.code,book.title);
fclose(cfPtr);
}
}
void edit()
{int no;
FILE*tempfile;
struct libraryInfo
{char code[10];
char title[50];
char author[20];
int no_copies;
};
fclose(cfPtr);
printf("Enter book code to be edited:\n");
tempfile=fopen("entry","w");
cfPtr=fopen("library.info","r");
while(fscanf(cfPtr,"%s%s",&book.title,&book.author)!=EOF)
{if(no==book.code)
{printf("Enter new Book Title:\n");
scanf("%s",&book.code);
printf("Enter new Book Author:\n");
scanf("%s",&book.title);
}
fprintf(tempfile,"%s\t\t%s",book.title,book.author);
}
fclose(temfile);
fclose(cfPtr);
rename("entry","library.info");
cfPtr=fopen("library.info","r");
}
void delete()
{int no;
char x;
FILE*tempfile;
printf("Enter book code to be deleted:\n");
scanf("%s",&book.code);
tempfile=fopen("entry","w");
cfPtr=fopen("library.info","r");
while(fscanf(cfPtr,"%s%s",&book.title,&book.author)!=EOF)
{if(no==book.code)
{printf("Do you really want to delete?\n");
x=getche();
if(to upper(x)=='Y')
break;
}
fprintf(tempfile,"%s\t\t%s",book.title,book.author);
}
fclose(tempfile);
fclose(cfPtr);
rename("entry","book");
cfPtr=fopen("library.info","r");
}
-->this one is done by my group.
this code dont have the one for Transaction...,but still it didn't
work..,