0

I am coding a program for a library management system as my project in c. I used a while loop along with !feof condition to check the file for book. Since I read that we should not use it, and if we must, we should do it with some break statement and also since I was reading large no. of variables from file, I used found for breaking the while loop. whenever the Target and book.bname will match (using strcmp), found is set to 1 and while loop will be terminated. This is the logic I used behind this code. Here book is a structure of which book.bname is a part. I am using linux os for this program. Please tell me where I am mistaking?

void Searchbook()
{
    int i;
    char Target[25],stats[3];
    int Found=0;
    if((librecord=fopen("librecord.txt","r"))==NULL)
        printf(" ! The File is Empty...\n\n");
    else
    {
        printf("\nEnter The Name Of Book : ");
        scanf("%s",Target);
        while(!feof(librecord)&& Found==0)
        {
              fscanf(librecord,"%d %s %s %d %d",&book.bid,book.bname,book.author,&book.status,&book.nooftitles);
              if(strcmp(Target,book.bname)==0)
                  Found=1;
              for(i=0;i<book.nooftitles;i++)
                  fscanf(librecord,"%s",book.titles); 
        }
        if(Found)
        { 
             if(book.status==IN)
                strcpy(stats,"IN");
             else
                strcpy(stats,"OUT");
             printf("\nThe Unique ID of The Book:  %d\nThe Name of Book is: %s\nThe Author is:  %s\nThe Book Status:%s\n\n",book.bid,book.bname,book.author,stats);
        }
        else if(!Found)
            printf("! There is no such Entry...\n");
        fclose(librecord);
    }
}

This is a function in which I am facing the infinite loop after entering the name of the book. It goes into an infinite loop printing the name of first book it encounters. What should i do? my problem is different than the other similar question since i am using two conditions instead of only !feof. I am using a variable as a flag .still the while loop is not terminating.

vc59
  • 3
  • 2
  • 8
    1) `feof()` is always wrong http://stackoverflow.com/q/5431941/905902 2) `stats[3];` is to small for "OUT". – wildplasser Nov 19 '16 at 20:24
  • How does code know `book,bname` in `if(strcmp(Target,book.bname)==0)` is valid after `scanf(librecord,"%d %s %s %d %d",&book.bid,book.bname ...`. Did that input operation succeed or fail? – chux - Reinstate Monica Nov 19 '16 at 20:33
  • 2
    One step would be to show some data that is being read. Another important step would be to test the return values of input functions. For example, you have `fscanf(librecord, "%d %s %s %d %d", &book.bid, book.bname, book.author, &book.status, &book.nooftitles);` — if the next character in the input is not a digit, the `fscanf()` will return 0, but won't affect the EOF status (it hasn't reached EOF; it simply can't process the next character as a number). Always check the input operation: `if (fscanf(librecord, "%d %s %s %d %d", …) != 5) { …oops!… }`. – Jonathan Leffler Nov 19 '16 at 20:34

1 Answers1

0

You are not reading the next line from the file as you are not advancing at all. You need to use fgets to read a string from the file or fgetc to read a char. fgets stops automatically when it reads a new line character so that could be a solution to read one line at a time. Another solution if you are using the GNU version of the C library is to use [getLine] (http://man7.org/linux/man-pages/man3/getdelim.3.html) which reads a line for you.

Whatever you use, you need to read a line and then move to the next line until the end is reached, which is not what you are doing with your current code.

amine.ahd
  • 431
  • 3
  • 11