这段代码给分割错误

问题描述 投票:-2回答:1

为什么此代码在printf("9\n")之后给出分段错误。基本上,while循环启动后,代码便崩溃了。谁能告诉我如何解决?考虑我叫headrvLoader()in main()`''>]

tetypedef struct cartbook
{
  int issue;
  char *id;
  struct cartbook *next;
}cartstruct;

struct cartbook *headrv;

void headrvLoader()
{
  FILE *fp1,*fp2;printf("1\n");
  struct cartbook *current,*prev,*p;printf("2\n");
  fp1=fopen("ReservedBooks.txt","r");printf("3\n");
  fp2=fopen("ReservedUserId.txt","r");printf("4\n");
  headrv=(struct cartbook*)malloc(sizeof(struct cartbook));printf("5\n");
  char *str;printf("6\n");
  current=(struct cartbook*)malloc(sizeof(struct cartbook));printf("7\n");
  headrv->next=current;printf("8\n");
  prev=headrv;printf("9\n");
  while(true)
  {
    if(fgets(current->id,50,fp2)==NULL)
    {
        printf("jhhj");
      current=NULL;
      prev->next=NULL;
    }
}
}
    

为什么此代码在printf(“ 9 \ n”)之后给出分段错误。基本上,while循环启动后,代码便崩溃了。谁能告诉我如何解决?考虑我叫headrvLoader()...

c gcc linked-list file-handling
1个回答
0
投票

fgets(current->id,.. id中未初始化。

© www.soinside.com 2019 - 2024. All rights reserved.