C语言链表(带头节点的链表)的文件读写与销毁

链表的写入文件

/*====================存储彩票信息========================*/
/*功能:将彩票的全部数据写入文件
*参数:彩票数据链表
*返回值:布尔类型,读出成功返回TRUE,否则返回FALSE
*/
boolean writeLotteryInfo(LOTTERY *head)
{
	FILE *fp;LOTTERY *current;
	boolean success=FALSE;
	current=head->next;
	if(current==NULL)
	{
		return FALSE;
	}
	if((fp=fopen("lotteryinfo.txt","wb+"))==NULL)
	{
		printf("打开文件出现错误。\n");
		exit(1);
	}
	while(current!=NULL)
	{
		if(fwrite(current,LOT_LEN,1,fp)==1)
		{
			success=TRUE;
			current=current->next;
		}
	}
	fclose(fp);
	return success;
}

从文件读出链表数据

/*====================读取彩票信息========================*/
/*功能:从文件里读出彩票的全部数据
*参数:彩票数据链表
*返回值:布尔类型,读出成功返回TRUE,否则返回FALSE
*/
boolean readLotteryInfo(LOTTERY *head)
{
	FILE *fp;LOTTERY *new_lottery,*current;
	boolean success=FALSE;
	current=head;
	if((fp=fopen("lotteryinfo.txt","rb+"))==NULL)
	{
		printf("打开文件出现错误。\n");
		exit(1);
	}
	while(!feof(fp))
	{
		new_lottery=(LOTTERY *)malloc(LOT_LEN);
		if(new_lottery==NULL)
		{
			printf("分配内存出现错误。\n");
			exit(1);
		}
		memset(new_lottery,'\0',LOT_LEN);
		if(fread(new_lottery,LOT_LEN,1,fp)==1)
		{
			success=TRUE;
			current->next=new_lottery;
			current=new_lottery;
		}
	}
	fclose(fp);
	return success;
}

链表的销毁

/*====================销毁彩票信息链表====================*/
/*功能:将全部彩票的数据的链表销毁
*参数:彩票数据链表
*返回值:布尔类型,读出成功返回TRUE,否则返回FALSE
*/
boolean destroyLotteryLink(LOTTERY *head)
{
	LOTTERY *current;
	boolean success=FALSE;
	if(head->next==NULL){
		success=TRUE;
	}
	else
	{
		current=head->next;
		while(current!=NULL)
		{
			head->next=current->next;
			free(current);
			current=NULL;
			current=head->next;
		}
	success=TRUE;
	}
	return success;
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值