使用带有终止条件的while(!feof),它仍然在c中无限循环 [英] using while(!feof) with terminating condition ,still it is going in infinite loop in c

查看:69
本文介绍了使用带有终止条件的while(!feof),它仍然在c中无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为c中的项目编写一个用于图书馆管理系统的程序.我使用了while循环以及!feof 条件来检查文件中的 book .既然我读到我们不应该使用它,并且如果必须的话,我们应该使用一些break语句来完成它,并且因为我读的是大号.从文件中获取变量,我使用 found 打破了while循环.只要 Target book.bname 匹配(使用 strcmp ), found 就会设置为1,并进行while循环将终止.这是我在此代码后面使用的逻辑.这里的 book book.bname 组成部分的结构.我正在为此程序使用linux os.请告诉我我要去哪里?

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);
    }
}

这是一个函数,在输入本书的名称后,我将面临无限循环.它进入一个无限循环,打印它遇到的第一本书的名称.我应该怎么办?我的问题不同于其他类似的问题,因为我使用的是两个条件,而不是仅使用!feof .我正在使用变量作为 flag .仍然, while循环不会终止.

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.

推荐答案

由于根本没有前进,因此您没有从文件中读取下一行.您需要使用 fgets 从文件中读取字符串,或者使用 fgetc 读取字符. fgets 在读取换行符时会自动停止,因此这可能是一次读取一行的解决方案.如果您正在使用C库的GNU版本,则另一个解决方案是使用[getLine](

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.

这篇关于使用带有终止条件的while(!feof),它仍然在c中无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆