从链表中读取文件和写文件

#ifndef __1_H_
#define __1_H_


#define SIZE (sizeof(struct tongxun)/sizeof(char))
typedef struct tongxun
{
char id[20];
char name[30];
long num;
char zhuzhi[100];
long tel;
struct tongxun * next;
}T;


void caidan();


T * create();




T * create()

{
FILE* fp;




T* head;
head=(T*)malloc(SIZE);
if(head==NULL)
{
printf("error\n");
return NULL;
}
head->next=NULL;


fp=fopen("1","r");
if(fp==NULL)
{
printf("open error\n");
return NULL;
}
T t;
T *p,*p1;
int n;
fread(&n,sizeof(int),1,fp);
count11=n;


int i;
for(i=0;i<n;i++)
{
fread(&t,sizeof(T),1,fp);
p1=head;
while(p1->next)
{
p1=p1->next;
}
p=(T*)malloc(sizeof(T));
p->next=NULL;
strcpy(p->id,t.id);
strcpy(p->name,t.name);
strcpy(p->zhuzhi,t.zhuzhi);
p->num=t.num;
p->tel=t.tel;
p1->next=p;


}
fclose(fp);
return head;

}


void baocun(T *head)
{
	FILE *fp;
	fp=fopen("1","w+");
	if(fp==NULL)
	{
		printf("open error\n");
		return;
	}
	fwrite(&count11,sizeof(int),1,fp);
	
	T* p;
	p=head;

	if(p->next==NULL)
	{
		printf("无人,不需写入\n");
		fclose(fp);
		return ;
	}
	
	p=p->next;
	while(p)
	{
		fwrite(p,sizeof(T),1,fp);
		p=p->next;
		
	}
	
	
	
	
	printf("保存成功\n");
	fclose(fp);
}






  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值