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 arr[100];

if (fp== NULL)

{

return 1;

}

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

{

fscanf(fp,"%d",&(arr[i].data));

printf("%d\n", arr[i].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;

}

相关推荐
知南x2 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言
2的n次方_4 小时前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫4 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫6 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
傻乐u兔7 小时前
C语言进阶————指针3
c语言·开发语言
CodeSheep程序羊8 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
I'mChloe8 小时前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
2的n次方_9 小时前
Runtime 内存管理深化:推理批处理下的内存复用与生命周期精细控制
c语言·网络·架构
嵌入小生0079 小时前
标准IO---核心函数接口延续(嵌入式Linux)
c语言·vscode·vim·嵌入式·小白·标准io·函数接口
历程里程碑10 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法