使用带有终止条件的 while(!feof) ,它仍然在 c 中进入无限循环
using while(!feof) with terminating condition ,still it is going in infinite loop in c
我正在为图书馆管理系统编写一个程序,作为我在 c 中的项目。我使用 while 循环和 !feof
条件来检查文件中的 book
。因为我读到我们不应该使用它,如果我们必须,我们应该用一些 break 语句来做,而且因为我正在阅读大量的 no。来自文件的变量,我使用 found
来打破 while 循环。每当 Target
和 book.bname
匹配时(使用 strcmp
),found
设置为 1 并且 while 循环将终止。这是我在这段代码背后使用的逻辑。这里 book
是一个结构,book.bname
是其中的一部分。我正在为这个程序使用 linux os。请告诉我哪里弄错了?
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
。我正在使用 variable
作为 flag
。while loop
仍然没有终止。
您没有阅读文件的下一行,因为您根本没有前进。
您需要使用 fgets
从文件中读取字符串或使用 fgetc
读取字符。
fgets
在读取换行符时自动停止,因此这可能是一次读取一行的解决方案。
如果您使用的是 C 库的 GNU 版本,另一种解决方案是使用 [getLine] (http://man7.org/linux/man-pages/man3/getdelim.3.html) 为您读取一行。
不管你用什么,你都需要读一行然后移动到下一行直到读到结尾,这不是你当前代码所做的。
我正在为图书馆管理系统编写一个程序,作为我在 c 中的项目。我使用 while 循环和 !feof
条件来检查文件中的 book
。因为我读到我们不应该使用它,如果我们必须,我们应该用一些 break 语句来做,而且因为我正在阅读大量的 no。来自文件的变量,我使用 found
来打破 while 循环。每当 Target
和 book.bname
匹配时(使用 strcmp
),found
设置为 1 并且 while 循环将终止。这是我在这段代码背后使用的逻辑。这里 book
是一个结构,book.bname
是其中的一部分。我正在为这个程序使用 linux os。请告诉我哪里弄错了?
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
。我正在使用 variable
作为 flag
。while loop
仍然没有终止。
您没有阅读文件的下一行,因为您根本没有前进。
您需要使用 fgets
从文件中读取字符串或使用 fgetc
读取字符。
fgets
在读取换行符时自动停止,因此这可能是一次读取一行的解决方案。
如果您使用的是 C 库的 GNU 版本,另一种解决方案是使用 [getLine] (http://man7.org/linux/man-pages/man3/getdelim.3.html) 为您读取一行。
不管你用什么,你都需要读一行然后移动到下一行直到读到结尾,这不是你当前代码所做的。