我试图从二进制文件中读取节点,但它只会读取第一个(我认为(。当 load(( 运行时,它显示以下内容: 仅加载执行的输出
请注意,两个 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 Namen");
scanf("%s",ul->uname);
printf("Enter Passwordn");
scanf("%s",ul->upass);
printf("Enter IDn");
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 Namen");
scanf("%s",ul->uname);
printf("Enter Passwordn");
scanf("%s",ul->upass);
printf("Enter IDn");
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:%snPassword:%snID:%dnList 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.loadn3.exitn2.displayn4.savenEnter 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(( 是注释形式,因为我只想使用它来更改数据库/文件。
我使用代码:块。我已经使用 C 大约一年半了,但对文件函数和链表相当陌生。改进代码的任何提示?
您需要在循环的每次迭代中创建新用户,同时从文件中读取用户:
删除这些行(非常不清楚,您想读取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;
}