简体   繁体   中英

Library management system using c

I had to make a library management system, just for practice and I made it (mostly). Whenever I re-execute the program the values given to the book id, name etc. are overwritten acc to the new run. How to prevent this and have data stored on from the last runs as well?

#include <stdio.h>
#include <stdlib.h>

typedef struct Book
{
 char *bname[40];
 int ssnid;
 int location;
 int issuedate;
 int issuemonth;
 int returndate;
 int returnmonth;
}book;
typedef struct Student
{
char *sname[30];
int sid;
int count;
}student;
void addbook(book lbook[10],int num)
{
int i;
FILE *fptr;
printf("enter the book name \t");
fflush(stdin);
gets(lbook[num].bname);
lbook[num].location=0;
lbook[num].issuedate=0;
lbook[num].issuemonth=0;
lbook[num].returndate=0;
lbook[num].returnmonth=0;
label:
printf("\nenter the SSN ID :\t");
scanf("%d",&lbook[num].ssnid);
for(i=0;i<num;i++)
{
    if(lbook[i].ssnid==lbook[num].ssnid)
     {
        printf("\nthis id has already been used\n");
        goto label;
     }
}
fptr=fopen("book.txt","a");
fprintf(fptr,"\nbook number %d \nbook name :\t",num+1);
fputs(lbook[num].bname,fptr);
fprintf(fptr,"\n book SSN ID %d",lbook[num].ssnid);
fclose(fptr);
}
  void addstudent(student lstudent[10],int snum)
 {
 int i;
 FILE *fptr;
 printf("enter the student name \t");
 fflush(stdin);
 gets(lstudent[snum].sname);
 lstudent[snum].count=0;
 label2:
 printf("\nenter the ID :\t");
 scanf("%d",&lstudent[snum].sid);
 for(i=0;i<snum;i++)
{
    if(lstudent[i].sid==lstudent[snum].sid)
    {
        printf("\nthis id has already been used\n");
        goto label2;
    }
}
fptr=fopen("student.txt","a");
fprintf(fptr,"\nstudent number %d \nstudent name :\t",snum+1);
fputs(lstudent[snum].sname,fptr);
fprintf(fptr,"\n student ID %d",lstudent[snum].sid);
fclose(fptr);
 }
  /*void delbook(book lbook[10],int num,int bnum)
 {
FILE *fptr;
int i,n;
for(n=num;n<bnum+1;n++)
{
    strcpy(lbook[10].bname,lbook[n].bname);
    lbook[10].ssnid=lbook[n].ssnid;
    strcpy(lbook[n].bname,lbook[n+1].bname);
    lbook[n].ssnid=lbook[n+1].ssnid;
    strcpy(lbook[n+1].bname,lbook[10].bname);
    lbook[n+1].ssnid=lbook[10].ssnid;

  }
  lbook[bnum].ssnid=0;
  fptr=fopen("book.txt","w");
for(i=0;i<bnum;i++)
{fprintf(fptr,"\nbook number %d \nbook name :\t",i+1);
fputs(lbook[i].bname,fptr);
fprintf(fptr,"\n book SSN ID %d",lbook[i].ssnid);
}
fclose(fptr);
}*/
 void issuebook(book lbook[10],student lstudent[10],int bnum,int snum)
{
int nb,i,ns,j;
int date,month;
FILE *cptr;
printf("\nenter book id to be issued\t");
scanf("%d",&nb);
for(i=0;i<bnum;i++)
{
    if(lbook[i].ssnid==nb)
    {
        if(lbook[i].location==0)
        {
            printf("\nenter the student id: \t");
        scanf("%d",&ns);
        for(j=0;j<snum;j++)
        {
            if(lstudent[j].sid==ns)
            {
                if(lstudent[j].count==0||lstudent[j].count==1)
                {
                    printf("\nenter date :\t");
                    scanf("%d",&lbook[i].issuedate);
                    printf("\nenter month :\t");
                    scanf("%d",&lbook[i].issuemonth);
                    lbook[i].location=lstudent[j].sid;
                lstudent[j].count++;
                cptr=fopen("log.txt","a");
                fprintf(cptr,"\nbook id %d issued by student id %d date on 
               %d month %d",lbook[i].ssnid,lstudent[j].sid,lbook[i].issuedate,lbook[i].issuemonth);
                fclose(cptr);
                }
            else{printf("\nlimit reached i.e. 2 books already issued\n");}
        }
    }
        }
    else{printf("book already issued\n");}
    }
   /* else
    {
        printf("\nbook not available\n");
    }*/
}
}

 void returnbook(book lbook[10],student lstudent[10],int bnum,int snum)
{
int nb,i,ns,j;
int date,month,dcount;
FILE *cptr;printf("\nenter book id to be returned\t");
scanf("%d",&nb);
for(i=0;i<bnum;i++)
{
    if(lbook[i].ssnid==nb)
    {
        if(lbook[i].location!=0)
        {
            printf("\nenter the student id: \t");
        scanf("%d",&ns);
        for(j=0;j<snum;j++)
        {
            if(lstudent[j].sid==ns)
            {
                //if(lstudent[j].count==0||lstudent[j].count==1)
                //{
                    printf("\nenter return date :\t");
                    scanf("%d",&lbook[i].returndate);
                    printf("\nenter return month :\t");
                    scanf("%d",&lbook[i].returnmonth);
                    printf("\nissue date \t%d",lbook[i].issuedate);
                    printf("\nissue month%d",lbook[i].issuemonth);
                    printf("\nenter days between issue and return\t");
                    scanf("%d",&dcount);
                    if(dcount>7)
                    {printf("fine :%d \t",(dcount-7)*20);}
                    lbook[i].location=0;
                lstudent[j].count--;
                cptr=fopen("log.txt","a");
  fprintf(cptr,"\nbook id %d returned by student id %d on date %d month 

 %d",lbook[i].ssnid,lstudent[j].sid,lbook[i].returndate,lbook[i].returnmonth
 );
if(dcount>7)
{fprintf(cptr,"\n fine payed of rs. %d ",(dcount-7)*20);}
                fclose(cptr);
                //}
             //else{printf("\nlimit reached i.e. 2 books already issued\n");}
        }
    }
        }
    else{printf("book already in library\n");}
    }
 }
 }
  void displaybook()
  {
  FILE *fptr;
  char ch;
  fptr=fopen("book.txt","r");
  ch=fgetc(fptr);
  while(ch!=EOF)
 {
    putchar(ch);
    ch=fgetc(fptr);
  }
  fclose(fptr);
  } 
  void displaystudent()
 {
FILE *fptr;
char ch;
fptr=fopen("student.txt","r");
ch=fgetc(fptr);
while(ch!=EOF)
{
    putchar(ch);
    ch=fgetc(fptr);
}
fclose(fptr);
}
int main()
{
book lbook[10];
student lstudent[10];
int bnum=0;
int snum=0;
char ch;
int i;
int delid;
int choice;
FILE *fptr;
fptr=fopen("book.txt","r");
for(i=0;i<10;i++)
{
    fscanf(fptr,"%d",lbook[i].ssnid);
    fscanf(fptr,"%d",lbook[i].issuedate);
    fscanf(fptr,"%d",lbook[i].issuemonth);
    fscanf(fptr,"%d",lbook[i].returndate);
    fscanf(fptr,"%d",lbook[i].returnmonth);
    fscanf(fptr,"%d",lbook[i].location);
    fgets(lbook[i].bname,1,fptr);
  }
  fclose(fptr);
  fptr=fopen("student.txt","r");
  for(i=0;i<10;i++)
 {
    fscanf(fptr,"%d",lstudent[i].sid);
    fscanf(fptr,"%d",lstudent[i].count);
    fgets(lstudent[i].sname,1,fptr);
 }
 fclose(fptr);
 printf("\t \t \tLIBRARY MANAGEMENT SYSTEM\n");
 do{
 printf("enter 1.for adding a book\tenter 2.for adding a student\n3.for 
deleting a book\t4.for issuing a book\n5.for returning book\n");
 printf("enter 6 display books\tenter 7 for display students\n");
 scanf("%d",&choice);
 switch(choice)
 {
 case 1:if(bnum<10)
{
    addbook(lbook,bnum);
    bnum++;
}
else{printf("\nbooks complete\n");}
    break;
case 2:if(snum<10)
{
    addstudent(lstudent,snum);
    snum++;
}
else{printf("\nstudents complete\n");}
    break;
  /* case 3: printf("\nenter id to delete\t");
    scanf("%d",&delid);
  delbook(lbook,delid,bnum);
  bnum--;
  break;*/
  case 4:issuebook(lbook,lstudent,bnum,snum);
    break;
    case 5:returnbook(lbook,lstudent,bnum,snum);
    break;
    case 6:displaybook();
        break;
    case 7:displaystudent();
        break;
    case 8:
        break;
}
printf("enter y to continue\t");
scanf("%s",&ch);
}while(ch=='y');
}

First off I'm just going to say you need Minimal, Complete, and Verifiable parts to your answer.

But moving on, I didn't read all your code but this is what I got from your question:

I'm pretty sure you are having trouble storing the records.

One option is to store it in a database like SQLite (it is stored locally as a file so no server is needed)

One problem is the file written in addbook ( 3 items) did not match what was attempted to be read in main ( 7 items).
Generally do not mix the use of scanf and fgets . Use fgets for all input and parse as needed with sscanf . Do not use gets .

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Book
{
    char bname[40];
    int ssnid;
    int location;
    int issuedate;
    int issuemonth;
    int returndate;
    int returnmonth;
}book;
typedef struct Student
{
    char sname[30];
    int sid;
    int count;
}student;

void addbook(book lbook[10],int num)
{
    char line[200] = "";
    int i;
    int result = 0;
    FILE *fptr;

    printf("enter the book name \t");
    fflush(stdout);
    fgets(lbook[num].bname, sizeof lbook[num].bname, stdin);
    lbook[num].bname[strcspn ( lbook[num].bname, "\n")] = '\0';//remove newline
    lbook[num].location=0;
    lbook[num].issuedate=0;
    lbook[num].issuemonth=0;
    lbook[num].returndate=0;
    lbook[num].returnmonth=0;

    while ( 1) {
        do {
            printf("\nenter the SSN ID :\t");
            fgets ( line, sizeof line, stdin);
            result = sscanf(line, "%d",&lbook[num].ssnid);
        } while ( result != 1);
        for(i=0;i<num;i++)
        {
            if(lbook[i].ssnid==lbook[num].ssnid)
            {
                printf("\nthis id has already been used\n");
                continue;
            }
        }
        break;
    }
    fptr=fopen("book.txt","a");
    fprintf(fptr,"%d ",lbook[i].ssnid);
    fprintf(fptr,"%d ",lbook[i].issuedate);
    fprintf(fptr,"%d ",lbook[i].issuemonth);
    fprintf(fptr,"%d ",lbook[i].returndate);
    fprintf(fptr,"%d ",lbook[i].returnmonth);
    fprintf(fptr,"%d ",lbook[i].location);
    fprintf(fptr,"%s\n",lbook[num].bname);
    fclose(fptr);
}

void addstudent(student lstudent[10],int snum)
{
    char line[200] = "";
    int i;
    int result = 0;
    FILE *fptr;

    printf("enter the student name \t");
    fflush(stdout);
    fgets(lstudent[snum].sname, sizeof lstudent[snum].sname, stdin);
    lstudent[snum].sname[strcspn ( lstudent[snum].sname, "\n")] = '\0';//remove newline
    lstudent[snum].count=0;
    while ( 1) {
        do {
            printf("\nenter the ID :\t");
            fgets ( line, sizeof line, stdin);
            result = sscanf(line, "%d",&lstudent[snum].sid);
        } while ( result != 1);
        for(i=0;i<snum;i++)
        {
            if(lstudent[i].sid==lstudent[snum].sid)
            {
                printf("\nthis id has already been used\n");
                continue;
            }
        }
        break;
    }
    fptr=fopen("student.txt","a");
    fprintf(fptr,"%d %d %s\n",lstudent[snum].sid, lstudent[snum].count, lstudent[snum].sname);
    fclose(fptr);
}

void issuebook(book lbook[10],student lstudent[10],int bnum,int snum)
{
    char line[200] = "";
    int nb,i,ns,j;
    int result = 0;
    FILE *cptr;
    do {
        printf("\nenter book id to be issued\t");
        fgets ( line, sizeof line, stdin);
        result = sscanf( line, "%d",&nb);
    } while ( result != 1);
    for(i=0;i<bnum;i++)
    {
        if(lbook[i].ssnid==nb)
        {
            if(lbook[i].location==0)
            {
                do {
                    printf("\nenter the student id: \t");
                    fgets ( line, sizeof line, stdin);
                    result = sscanf(line, "%d",&ns);
                } while ( result != 1);
                for(j=0;j<snum;j++)
                {
                    if(lstudent[j].sid==ns)
                    {
                        if(lstudent[j].count==0||lstudent[j].count==1)
                        {
                            do {
                                printf("\nenter date :\t");
                                fgets ( line, sizeof line, stdin);
                                result = sscanf(line, "%d",&lbook[i].issuedate);
                            } while ( result != 1);
                            do {
                                printf("\nenter month :\t");
                                fgets ( line, sizeof line, stdin);
                                result = sscanf(line, "%d",&lbook[i].issuemonth);
                            } while ( result != 1);
                            lbook[i].location=lstudent[j].sid;
                            lstudent[j].count++;
                            cptr=fopen("log.txt","a");
                            fprintf(cptr,"\nbook id %d issued by student id %d date on %d month %d"
                            ,lbook[i].ssnid,lstudent[j].sid,lbook[i].issuedate,lbook[i].issuemonth);
                            fclose(cptr);
                        }
                        else{
                            printf("\nlimit reached i.e. 2 books already issued\n");
                        }
                    }
                }
            }
            else{printf("book already issued\n");}
        }
    }
}

void returnbook(book lbook[10],student lstudent[10],int bnum,int snum)
{
    int nb,i,ns,j;
    int dcount;
    int result = 0;
    char line[200] = "";
    FILE *cptr;
    do {
        printf("\nenter book id to be returned\t");
        fgets ( line, sizeof line, stdin);
        result = sscanf(line, "%d",&nb);
    } while ( result != 1);
    for(i=0;i<bnum;i++)
    {
        if(lbook[i].ssnid==nb)
        {
            if(lbook[i].location!=0)
            {
                do {
                    printf("\nenter the student id: \t");
                    fgets ( line, sizeof line, stdin);
                    result = sscanf(line, "%d",&ns);
                } while ( result != 1);
                for(j=0;j<snum;j++)
                {
                    if(lstudent[j].sid==ns)
                    {
                        do {
                            printf("\nenter return date :\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&lbook[i].returndate);
                        } while ( result != 1);
                        do {
                            printf("\nenter return month :\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&lbook[i].returnmonth);
                        } while ( result != 1);
                        printf("\nissue date \t%d",lbook[i].issuedate);
                        printf("\nissue month%d",lbook[i].issuemonth);
                        do {
                            printf("\nenter days between issue and return\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&dcount);
                        } while ( result != 1);
                        if(dcount>7)
                        {
                            printf("fine :%d \t",(dcount-7)*20);
                        }
                        lbook[i].location=0;
                        lstudent[j].count--;
                        cptr=fopen("log.txt","a");
                        fprintf(cptr,"\nbook id %d returned by student id %d on date %d month %d"
                        ,lbook[i].ssnid,lstudent[j].sid,lbook[i].returndate,lbook[i].returnmonth
                        );
                        if(dcount>7)
                        {
                            fprintf(cptr,"\n fine payed of rs. %d ",(dcount-7)*20);
                        }
                        fclose(cptr);
                    }
                }
            }
            else{printf("book already in library\n");}
        }
    }
}

void displaybook()
{
    FILE *fptr;
    char line[200] = "";
    if ( NULL != ( fptr=fopen("book.txt","r"))) {
        while(fgets ( line, sizeof line, fptr))
        {
            printf ( "%s",line);
        }
        fclose(fptr);
    }
    else {
        printf ( "no books to show\n");
    }
}

void displaystudent()
{
    FILE *fptr;
    char line[200] = "";
    if ( NULL != ( fptr=fopen("student.txt","r"))) {
        while(fgets ( line, sizeof line, fptr))
        {
            printf ( "%s",line);
        }
        fclose(fptr);
    }
    else {
        printf ( "no students to show\n");
    }
}

int main()
{
    book lbook[10];
    student lstudent[10];
    int bnum=0;
    int snum=0;
    char line[200] = "";
    int i;
    FILE *fptr;
    if ( NULL != ( fptr=fopen("book.txt","r"))) {
        i = 0;
        while ( i < 10 && fgets ( line, sizeof line, fptr)) {
            if ( 7 == sscanf(line,"%d %d %d %d %d %d %39[^\n]"
            , &lbook[i].ssnid
            , &lbook[i].issuedate
            , &lbook[i].issuemonth
            , &lbook[i].returndate
            , &lbook[i].returnmonth
            , &lbook[i].location
            , lbook[i].bname)) {
                i++;
            }
        }
        fclose(fptr);
    }
    if ( NULL != ( fptr=fopen("student.txt","r"))) {
        i = 0;
        while ( i < 10 && fgets ( line, sizeof line, fptr)) {
            if ( 3 == sscanf(line,"%d %d %29[^\n]"
            , &lstudent[i].sid
            , &lstudent[i].count
            , lstudent[i].sname)) {
                i++;
            }
        }
        fclose(fptr);
    }
    do{
        printf("\n\n\t\t\tLIBRARY MANAGEMENT SYSTEM\n");
        printf ( "\t\t1 (for adding a book)\n\t\t2 (for adding a student)\n");
        printf ( "\t\t3 (for deleting a book)\n\t\t4 (for issuing a book)\n\t\t5 (for returning book)\n");
        printf ( "\t\t6 (display books)\n\t\t7 (for display students)\n\t\t8 ( quit)\n");
        printf("\tchoice: ");
        fflush ( stdout);
        fgets ( line, sizeof line, stdin);
        switch( line[0])
        {
            case '1':
                if(bnum<10)
                {
                    addbook(lbook,bnum);
                    bnum++;
                }
                else{printf("\nbooks complete\n");}
                break;
            case '2':
                if(snum<10)
                {
                    addstudent(lstudent,snum);
                    snum++;
                }
                else{printf("\nstudents complete\n");}
                break;
            case '4':
                issuebook(lbook,lstudent,bnum,snum);
                break;
            case '5':
                returnbook(lbook,lstudent,bnum,snum);
                break;
            case '6':
                displaybook();
                break;
            case '7':
                displaystudent();
                break;
            case '8':
                return 0;
            default:
                printf ( "\n\tnot a valid choice\n\n");
                break;
        }
        printf("enter y to continue\t");
        fgets ( line, sizeof line, stdin);
    }while(line[0]=='y');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM