IO-DAY1

1.用fprintf将链表数据保存到文件中

2用fscanf将文件中数据写入链表

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>

typedef int datatype;

typedef struct link_list

{

union

{

int len;

datatype data;

};

struct link_list *next;

}link_list,*link_p;

link_p create_head()

{

link_p L = (link_p)malloc(sizeof(link_list));

if(L==NULL)

{

printf("空间申请失败\n");

return NULL;

}

L->len=0;

L->next=NULL;

return L;

}

//创建结点

link_p create_node(datatype data)

{

link_p new = (link_p)malloc(sizeof(link_list));

if(new==NULL)

{

printf("空间申请失败\n");

return NULL;

}

new->data = data;

return new;

}

//头插

void insert_head(link_p H,datatype data)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

link_p new = create_node(data);

new->next = H->next;

H->next = new;

H->len++;

}

void out_put_link(link_p H)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

link_p p = H->next;

while(p!=NULL)

{

printf("%d->",p->data);

p = p->next;

}

printf("NULL\n");

void save(char *filename)

{

FILE *w=fopen(filename,"w");

if (w == NULL)

{

return 1;

}

link_p p=H->next;

for (int i = 0; i <H->len; i++)

{

fprintf(w,"%d",p->data);

p=p->next;

}

fclose(w);

}void show(char *filename)/*输出文件*/

{

FILE *fp=fopen(filename,"r");

link_list arr100;

if (fp== NULL)

{

return 1;

}

for (int i = 0; i <H->len; i++)

{

fscanf(fp,"%d",&(arri.data));

printf("%d\n", arri.data);

}

fclose(fp);

}

int main(int argc, char *argv\[\])

{

link_p H=create_head();

insert_head(H,1);

insert_head(H,3);

insert_head(H,5);

insert_head(H,7);

insert_head(H,9);

out_put_link(H);

save(filename);

free(p);

show(filename);

return 0;

}

相关推荐
Canmag 兴隆磁性2 小时前
C 系列充磁机
c语言·开发语言·学习·永磁材料·充磁·退磁
_Narcissus_3 小时前
排序算法总结表格
c语言·数据结构·c++·笔记·算法·排序算法·总结
小雪崩3 小时前
嵌入式学习 day27:标准IO
linux·c语言·学习
xx~t5 小时前
嵌入式——Linux软件编程2
linux·运维·服务器·c语言·学习方法
luj_17688 小时前
变频技术核心原理揭秘
服务器·c语言·开发语言·经验分享·算法
_Narcissus_8 小时前
B树概念及操作笔记(含完整代码实现)
c语言·数据结构·数据库·c++·笔记·b树·算法
松小白song8 小时前
嵌入式通信协议全解:UART、RS232、RS485、I²C、SPI 与 CAN 的本质区别与选型指南
c语言·开发语言
Fa_Mian_Tuan9 小时前
图论基础 | 邻接多重表原理剖析 + 完整可运行C语言代码(插边/删边/释放)
c语言·数据结构·笔记·算法·图论
_Narcissus_9 小时前
B+树的概念和操作笔记(含完整代码实现)
c语言·数据结构·数据库·c++·笔记·b树·算法
l1t9 小时前
DeepSeek总结的DuckDB C-API-V2:第一部分 - #24702 PR
c语言·数据库·duckdb