You are on page 1of 10

Library database student and book details program in c using file..?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//*****BOOK DATABASE*****
typedef struct book
{
char bname[25];
char aname[25];
char subject[10];
char code[6];
char issto[6];
int price;
char desc[70];
int status;
}book;
//*****STUDENT DATABASE******
typedef struct stud
{
char studname[30];
char id[6];
float tot_fine;
int count;
char dept[5];
int year;
}stud;
long int getsize(char fname[])
{
FILE *fp;
char c;
long int n;
fp=fopen(fname,"r");
if(fp==NULL)
return 0;
fseek(fp,0,SEEK_END);
n=ftell(fp);
return n;
}
//****************************************
//******EDIT BOOK /ADD / REMOVE BOOKS*****
//****************************************
void editbook()
{
char code[6];
int choice;
FILE *fp;
book *bk;
long int size;
int n,i,j,c,flag=0;
size=getsize("book.txt")/sizeof(book);
printf("Enter your choice\n1. Add book\n2. remove book\n");
scanf("%d",&choice);
switch(choice)
{

case 1:
fp=fopen("book.txt","a+");
printf("%d\n",size);
printf("Enter the no. of books you want to enter\n");
scanf("%d",&n);
bk=(book *)calloc((size+n),sizeof(book));
fread(bk,sizeof(book),size,fp);
for(i=0;i<size;i++)
{
printf("%d\t",(bk+i)->price);
}
fclose(fp);
//bk=(book *)malloc((size+n)*sizeof(book));
fp=fopen("book.txt","w");
for(i=size;i<size+n;i++)
{
printf("Enter the Book Id\n");
scanf("%s",(bk+i)->code);
c=0;
// bk1=(book *)calloc(size,sizeof(book));
//fread(bk1,sizeof(book),size,fp);
for(j=0;j<size;j++)
{
if(strcmp((bk+i)->code,(bk+j)->code)==0)
{
printf("Code already exist\n");
c=1;
}
}
if(c==1)
{
i--;
continue;
}
printf("Enter the name of the book\n");
scanf("%s",(bk+i)->bname);
printf("Enter the author name\n");
scanf("%s",(bk+i)->aname);
printf("Enter the subject\n");
scanf("%s",(bk+i)->subject);
fflush(stdin);
printf("Enter the price of the book\n");
scanf("%d",&(bk+i)->price);
printf("%d",(bk+i)->price);
fflush(stdin);
printf("Write some description about it\n");
scanf("%s",(bk+i)->desc);
printf("%d",(bk+i)->price);
(bk+i)->status=0;
//fprintf(fp,"%s%s%s%s%f%s",(bk+i)->code,(bk+i)->bname,(bk+i)>aname,(bk+i)->subject,(bk+i)->price,(bk+i)->desc);
}
fwrite(bk,sizeof(book),(size+n),fp);
fclose(fp);
break;

case 2:
fp=fopen("book.txt","r");
bk=(book *)calloc(size,sizeof(book));
fread(bk,sizeof(book),size,fp);
printf("Enter the book code to delete \n");
scanf("%s",code);
for(i=0;i<size;i++)
{
printf("entered");
if(strcmp(code,(bk+i)->code)==0)
{
flag=1;
}
if(flag!=1)
{
if((bk+i)->status='a')
{
for(j=i;j<size-1;j++)
{
*(bk+j)= *(bk+j+1);
}
size=size-1;
}
else
{
printf("Book cannot be deleted it is issued\n");
return;
}
fclose(fp);
fp=fopen("book.txt","w+");
fwrite(bk,sizeof(book),size,fp);
printf("book deleted successfully\n");
return;
break;
}
else
{
printf(" Invalid book\n");
return;
}
}
}
//fwrite(bk,sizeof(book),(size+n),fp);
}
//*******************************************
//*******STUDENT EDIT************************
//*******************************************
void editstudent()
{
char id[6];
int choice;
FILE *fp;
stud *bk;
long int size;
int n,i,j,c,flag=0;

size=getsize("student.txt")/sizeof(stud);
printf("Enter your choice\n1. Add Student\n2. Remove Student\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
fp=fopen("student.txt","a+");
printf("%d\n",size);
printf("Enter the no. of student you want to enter\n");
scanf("%d",&n);
bk=(stud *)calloc((size+n),sizeof(stud));
fread(bk,sizeof(stud),size,fp);
for(i=0;i<size;i++)
{
printf("%s\t",(bk+i)->id);
}
fclose(fp);
//bk=(book *)malloc((size+n)*sizeof(book));
fp=fopen("student.txt","w");
for(i=size;i<size+n;i++)
{
printf("Enter the student Id\n");
scanf("%s",(bk+i)->id);
c=0;
// bk1=(book *)calloc(size,sizeof(book));
//fread(bk1,sizeof(book),size,fp);
for(j=0;j<size;j++)
{
if(strcmp((bk+i)->id,(bk+j)->id)==0)
{
printf("Student ID already exist\n");
c=1;
}
}
if(c==1)
{
i--;
continue;
}
printf("Enter the name of the Student\n");
scanf("%s",(bk+i)->studname);
printf("Enter the department\n");
scanf("%s",(bk+i)->dept);
fflush(stdin);
printf("Enter year\n");
scanf("%d",&(bk+i)->year);
/*printf("Enter the price of the book\n");
scanf("%d",&(bk+i)->price);
printf("%d",(bk+i)->price);
fflush(stdin);
printf("Write some description about it\n");
scanf("%s",(bk+i)->desc);
printf("%d",(bk+i)->price);
(bk+i)->status='a';*/
//fprintf(fp,"%s%s%s%s%f%s",(bk+i)->code,(bk+i)->bname,(bk+i)-

>aname,(bk+i)->subject,(bk+i)->price,(bk+i)->desc);
(bk+i)->tot_fine=0.0;
(bk+i)->count=0;
}
fwrite(bk,sizeof(stud),(size+n),fp);
fclose(fp);
break;
case 2:
fp=fopen("student.txt","r");
bk=(stud *)calloc(size,sizeof(stud));
fread(bk,sizeof(stud),size,fp);
printf("Enter the book code to delete \n");
scanf("%s",id);
for(i=0;i<size;i++)
{
printf("entered");
if(strcmp(id,(bk+i)->id)==0)
{
flag=1;
}
if(flag!=1)
{
if((bk+i)->count==0)
{
for(j=i;j<size-1;j++)
{
*(bk+j)= *(bk+j+1);
}
size=size-1;
}
else
{
printf("student cannot be deleted it is issued\n");
return;
}
fclose(fp);
fp=fopen("student.txt","w");
fwrite(bk,sizeof(stud),size,fp);
printf("Student deleted successfully\n");
return;
break;
}
else
{
printf(" Invalid book\n");
return;
}
}
}
//fwrite(bk,sizeof(book),(size+n),fp);
}
//*****************************************
//***********ISSUE BOOK********************
//*****************************************
void issue()

{
char bno[6],sno[6];
int bsize,i,flag=0,ssize,j,flag1=0,flag2=0,flag3=0;
char msg[22];
FILE *fp,*fp1;
book *bk;
stud *st;
flag1=flag2=flag=flag3=0;
strcpy(msg,"Enter the book number:");
printf("\n%s",msg);
scanf("%s",bno);
bsize=getsize("book.txt")/sizeof(book);
if(bsize==0)
{
printf("\nBooklist does not exist!");
getch();
return;
}
fp=fopen("book.txt","r+");
bk=(book*)calloc(bsize,sizeof(book));
fread(bk,sizeof(book),bsize,fp);
fclose(fp);
for(i=0;i<bsize;i++)
{
int temp;
temp=strcmp(bno,(bk+i)->code);
if(temp==0)
{
flag=1;
break;
}
}
if((bk+i)->status==0)
flag3=1;
if(flag==1 && flag3==1)
{
printf("Enter the student no:");
scanf("%s",sno);
ssize=getsize("student.txt")/sizeof(stud);
fp1=fopen("student.txt","r+");
st=(stud*)calloc(ssize,sizeof(stud));
fread(st,sizeof(stud),ssize,fp1);
for(j=0;j<ssize;j++)
{
int temp;
printf("%s",(st+j)->id);
temp=strcmp(sno,(st+j)->id);
if(temp==0)
{
flag1=1;
break;
}
}
if(flag1==1&&flag==1)
{
if((st+j)->count<5)
{
flag2=1;
fp=fopen("book.txt","w+");

strcpy((bk+i)->issto,sno);
(bk+i)->status=1;
fwrite(bk,sizeof(book),bsize,fp);
fp1=fopen("student.txt","w+");
(st+j)->count+=1;
fwrite(st,sizeof(stud),ssize,fp1);
}
}
}
if(flag!=1)
printf("\nInvalid Book Number!");
else if(flag3!=1)
printf("\nBook has aldready been issued!");
else if(flag1!=1)
printf("\nInvalid student number!");
else if(flag2!=1)
printf("\nTokens Exhausted!");
}
//*******************************************
//*********STUDENT STATUS********************
//*******************************************
void studstat()
{
FILE *bfp, *sfp;
book *bk;
stud *st;
char id[6];
int i,j,c=0;
long int bsize,ssize;
bsize=getsize("book.txt");
ssize=getsize("student.txt")/sizeof(stud);
bsize=getsize("book.txt")/sizeof(book);
bfp=fopen("book.txt","r");
sfp=fopen("student.txt","r");
st=(stud *)calloc(ssize,sizeof(stud));
fread(st,sizeof(stud),ssize,sfp);
printf("enter student id\n");
scanf("%s",id);
for(i=0;i<ssize;i++)
{
if(strcmp(id,(st+i)->id)==0)
{
c=1;
break;
}
}
if(c==1)
{
printf("\t\t\t\tSTUDENT DETAILS");
printf("\n\nStudent name:%s",(st+i)->studname);
printf("\nDepartment:%s",(st+i)->dept);
printf("\nTokens Used: %d",(st+i)->count);
printf("\nFine:%.2f",(st+i)->tot_fine);
if((st+i)->count==0)
{
return;
}

else if((st+i)->count!=0)
{
bsize=getsize("book.txt")/sizeof(book);
bk=(book*)calloc(ssize,sizeof(book));
fread(bk,sizeof(book),bsize,bfp);
fclose(bfp);
printf("\n\nBOOK(S) BORROWED FROM THE LIBRARY:\n");
for(i=0;i<bsize;i++)
{
if(strcmp(id,(bk+i)->issto)==0 && (bk+i)->status=='a')
{
printf("\nBook.No:%s\tBook Name:%s\n",(bk+i)->code,(bk+i)->bname);
}
}
}
}
}
//************************************************
//****************BOOK STATUS*********************
//************************************************
void bookstat()
{
FILE *bfp, *sfp;
book *bk;
stud *st;
char id[6],code[10];
int i,j,c=0;
long int bsize,ssize;
bsize=getsize("book.txt");
ssize=getsize("student.txt")/sizeof(stud);
bsize=getsize("book.txt")/sizeof(book);
bfp=fopen("book.txt","r");
sfp=fopen("student.txt","r");
st=(stud *)calloc(ssize,sizeof(stud));
bk=(book *)calloc(bsize,sizeof(book));
fread(st,sizeof(stud),ssize,sfp);
fread(bk,sizeof(book),bsize,bfp);
printf("enter book id\n");
scanf("%s",code);
for(i=0;i<bsize;i++)
{
if(strcmp(code,(bk+i)->code)==0)
c=1;
break;
}
if(c==1)
{
printf("\t\t\t\tBOOK DETAILS");
printf("\n\nBook name:%s",(bk+i)->bname);
printf("\nAuthor:%s",(bk+i)->aname);
printf("\nsubject: %s",(bk+i)->subject);
printf("\nCost:%.2f",(bk+i)->price);
printf("\navailability %d",(bk+i)->status);
if((bk+i)->status==0)
{
return;
}

else if((st+i)->count!=0)
{
printf("\n\nBOOK(S) BORROWED FROM THE LIBRARY BY:\n");
for(i=0;i<ssize;i++)
{
if(strcmp((st+i)->id,(bk+i)->issto)==0)
{
printf("\nstudent No:%s\tstudent Name:%s\n",(st+i)->id,(st+i)->studname);
}
}
}
}
}
//************************************************
//************BOOK RETURN*************************
//************************************************
void ret()
{
char bno[6],sno[6];
int size,bsize,i,flag=0,ssize,j,flag1=0,flag2=0,flag3=0;
char msg[22];
FILE *fp,*fp1;
book *bk;
stud *st;
flag1=flag2=flag=flag3=0;
strcpy(msg,"Enter the book number:");
printf("\n%s",msg);
scanf("%s",bno);
bsize=getsize("book.txt")/sizeof(book);
if(bsize==0)
{
printf("\nBooklist does not exist!");
return;
}
fp=fopen("book.txt","r+");
bk=(book*)calloc(bsize,sizeof(book));
fread(bk,sizeof(book),bsize,fp);
fclose(fp);
for(i=0;i<bsize;i++)
{
int temp;
temp=strcmp(bno,(bk+i)->code);
if(temp==0)
{
flag=1;
break;
}
}
if((bk+i)->status==1)
flag3=1;
if(flag==1 && flag3==1)
{
printf("Enter the student no:");
scanf("%s",sno);
ssize=getsize("student.txt")/sizeof(stud);
fp1=fopen("student.txt","r+");
st=(stud *)calloc(ssize,sizeof(stud));
fread(st,sizeof(stud),ssize,fp1);
for(j=0;j<ssize;j++)

{
int temp;
temp=strcmp(sno,(st+j)->id);
if(temp==0)
{
flag1=1;
break;
}
}
if(flag1==1&&flag==1)
{
fp=fopen("book.txt","w+");
strcpy((bk+i)->issto,"");
(bk+i)->status=0;
fwrite(bk,sizeof(book),size,fp);
fp=fopen("student.txt","w+");
(st+j)->count-=1;
fwrite(st,sizeof(stud),ssize,fp1);
}
}
if(flag!=1)
printf("\nInvalid Book Number!");
else if(flag3!=1)
printf("\nBook not issued issued!");
else if(flag1!=1)
printf("\nInvalid student number!");
else if(flag2!=1)
printf("\nTokens Exhausted!");
}
void main()
{
char ch;
printf("enter choice..\n1.edit student\n2.edit book\n3.student status\n4.book st
atus\n5.issue book 6.return book\n");
ch=getche();
switch(ch)
{
case '1':editstudent();
break;
case '2':editbook();
break;
case '3':studstat();
break;
case '4':bookstat();
break;
case '5':issue();
break;
case '6':ret();
break;
}
getch();
}