Lofty0107 2022-04-08 14:18 采纳率: 100%
浏览 117
已结题

fscanf函数出现死循环,如何解决

问题遇到的现象和发生背景

写将文件中数据导入进链表的函数,使用了fscanf的循环,结果执行到这个循环程序就卡死,反复debug后发现执行完一次fscanf循环后第二次无法开始,如何解决?
问题相关代码,请勿粘贴截图

typedef struct HouseSource //房源的链表
{
char name[20]; //房源名
int price; //房源价格
int start; //起租年份
int end; //退租年份
struct HouseSource next;
} housesource;
void init()
{
FILE
fp=NULL;
h1=head_h;
housesource* house=h1;
fp=fopen("./housesource.txt","r");
bool t_repeat;
char t_name[20];
int t_price;
int t_start;
int t_end;
while(!feof(fp))
{
if(fscanf(fp,"%s %d %D %d\n",t_name,&t_price,&t_start,&t_end)==0) //此处大写D是因为csdn不允许重复字符
{
fseek(fp,2,1);
}
else
{
h1=(housesource*)malloc(sizeof(housesource));
strcpy(h1->name,t_name);
h1->price=t_price;
h1->start=t_start;
h1->end=t_end;
h1->next=NULL;
house=(housesource*)malloc(sizeof(housesource));
house->next=h1;
house=h1;
}
}
}
void allocation()
{
head_h=(housesource*)malloc(sizeof(housesource));
head_h->next=NULL;
init();
}
int main()
{
allocation();
return 0;
}

运行结果及报错内容

img


相当于没有结果

我的解答思路和尝试过的方法

上网查找了很多关于fscanf函数的内容,试图用fseek跳过,试图改循环判定,均未果,已经束手无策了
我想要达到的结果

循环能完全读取文件中的数据

  • 写回答

2条回答 默认 最新

  • 关注

    fscanf(fp,"%s %d %D %d\n",t_name,&t_price,&t_start,&t_end)==0这里应该是 <=0 吧,读取失败返回-1
    h1和head_h这两个是全局变量吗?也没看到你声明这两个变量。
    建立链表的逻辑也是错误的,代码修改如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct HouseSource //房源的链表
    {
        char name[20]; //房源名
        int price; //房源价格
        int start; //起租年份
        int end; //退租年份
        struct HouseSource* next;
    } housesource;
    
    housesource *head_h,*h1;
    void init()
    {
        FILE* fp = NULL;
        h1 = head_h;
        housesource* house = h1;
        fp = fopen("./housesource.txt", "r");
        bool t_repeat;
        char t_name[20];
        int t_price;
        int t_start;
        int t_end;
        while (!feof(fp))
        {
            if (fscanf(fp, "%s %d %d %d\n", t_name, &t_price, &t_start, &t_end) <= 0) //
            {
                fseek(fp, 2, 1);
            }
            else
            {
                h1 = (housesource*)malloc(sizeof(housesource));
                strcpy(h1->name, t_name);
                h1->price = t_price;
                h1->start = t_start;
                h1->end = t_end;
                h1->next = NULL;
                //house = (housesource*)malloc(sizeof(housesource));
                house->next = h1;
                house = h1;
            }
        }
    }
    void allocation()
    {
        head_h = (housesource*)malloc(sizeof(housesource));
        head_h->next = NULL;
        init();
    }
    int main()
    {
        allocation();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 修改了问题 4月8日
  • 修改了问题 4月8日
  • 展开全部

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备