从二进制文件读取用户信息到链接列表

时间:2018-10-03 18:01:05

标签: c linked-list structure fread

我试图从二进制文件中读取节点,但是它只会读取第一个(我认为)。当运行load()时,它将显示以下内容: Output of only load execution

请注意,这两个printf仅用于检查其是否正确读取。在最终版本中将不会存在,就像void save()中的scanf()语句一样。 这样做的目的仅仅是将数据加载并保存到文件中

代码:

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>

//function prototypes
struct user* newuser();
void save();
void load();
void display();


struct user
{
    char uname[50];
    char upass[15];
    int uid;
    int listofbooks[3];
    struct user *next;
};
struct user *ustart=NULL;

/*struct book
{
    int bid;
    char topic[15];
    char aut[50];
    char bname[50];
    struct book *l;
    struct book *r;
};  */

struct user* newuser()
{
  struct user *u;
  u=(struct user* )malloc(sizeof(struct user))  ;
  return(u);
};

/*void save()
{
 struct user *ul;
 struct user *t;
 int i;
 FILE *fp;

 ul=newuser();
 if(ustart==NULL)
 {
     ustart=ul;
     ul->next=NULL;

     printf("Enter Name\n");
   scanf("%s",ul->uname);
   printf("Enter Password\n");
   scanf("%s",ul->upass);
   printf("Enter ID\n");
   scanf("%d",&ul->uid);
   for(i=0;i<4;i++)
   {
       ul->listofbooks[i]=0;
   }

   if( ( fp=fopen("ham.txt","wb+") )==NULL )
  {
      printf("FIle opening error");
      getch();
  }
   fwrite(ul,sizeof(struct user),1,fp);
   fclose(fp);
 }
 else
 {
   t=ustart;
   while(t->next!=NULL){
    t=t->next;
   }
   //change
   t->next=ul;
   ul->next=NULL;
   printf("Enter Name\n");
   scanf("%s",ul->uname);
   printf("Enter Password\n");
   scanf("%s",ul->upass);
   printf("Enter ID\n");
   scanf("%d",&ul->uid);
   for(i=0;i<4;i++)
   {
       ul->listofbooks[i]=0;
   }

    if( ( fp=fopen("ham.txt","rb+") )==NULL )
  {
       printf("FIle opening error");
   }
    fseek(fp, 0, SEEK_END);

   fwrite(ul,sizeof(struct user),1,fp);
   fclose(fp);
 }
}*/



void load()
{
 int i;
 long int j;
 struct user *ul;
 FILE *fp;

 ul=newuser();
 ustart=ul;

  if( ( fp=fopen("ham.txt","rb+") )==NULL )
  {
      printf("FIle opening error");
      getch();
 }
  fseek(fp,0,SEEK_END);
  j=ftell(fp);
  j=( j/sizeof(struct user)) ;

   for(i=0;i<j;i++)
   {
    fseek(fp, (sizeof(struct user) * i), SEEK_SET);
    fread(ul,sizeof(struct user),1,fp);
    printf("\nName:%s",ul->uname);
    printf("\nPassword:%s",ul->upass);
    ul=ul->next;
   }

   fclose(fp);

}

void display()
{
    struct user *t;
    int i;
    t=ustart;
    while(t!=NULL)
    {
    printf("Name:%s\nPassword:%s\nID:%d\nList of books",t->uname,t->upass,t->uid);
    for(i=0;i<4;i++){
    printf("%d",t->listofbooks[i]);
    }
  }
}

void main()
{
   int i,lp=1;
   do{
    printf("\n1.load\n3.exit\n2.display\n4.save\nEnter choice:");
    scanf("%d",&i);
    switch(i)
    {
        case 1: load();
        break;

    case 2:display();
    break;

    case 3:lp=0;
    printf("Exiting");
    break;

    //case 4:save();
    //break;

    default:printf("invalid");
    break;
    }
     }while(lp);

    }

请帮助找到代码中的错误。我检查了许多站点尝试了许多不同的迭代fread()的方法,但均无济于事。

我不认为fwrite有问题,因为当我打开txt文件时字符串成员在那里。(void save()为注释形式,因为我希望仅使用它来更改数据库/文件。)

我使用code:blocks。我已经使用C大约一年半了,但是对于文件功能和链接列表还是很新的。是否有任何改进代码的技巧?

1 个答案:

答案 0 :(得分:1)

您需要在循环的每个迭代中创建新用户,同时从文件中读取用户:

删除这些行(目前尚不清楚,您想读取j个用户,但只为其中一个分配内存):

ul=newuser();
ustart=ul;

添加以下更改:

   user* previousUser = NULL; // to hold previous user from file

   for(i=0;i<j;i++)
   {
        ul=newuser(); // create new user with every iteration
        if (!i)
            ustart = ul; // set head of list

        fseek(fp, (sizeof(struct user) * i), SEEK_SET);
        fread(ul,sizeof(struct user),1,fp);
        printf("\nName:%s",ul->uname);
        printf("\nPassword:%s",ul->upass);

        ul->next = NULL; // set next to NULL for current user
        if (previousUser)
            previousUser->next = ul; // update previous user to point to current one
        previousUser = ul;
   }