C语言中如何将链表中的数据写入文件中

2025-01-01 03:13:24
推荐回答(1个)
回答1:

#include
#include

typedef struct LinkedList_st{
int key;
struct LInkedList_st * next;

}LinkedList;

int writeList(const char* path,LinkedLIst* lst){
if(lst==NULL) return -1;
FILE* file = open(path,'w+');
if(file==NULL){
printf("open file failed!");
return -1;

}

LinkedList* ptr = lst;

while(ptr!=NULL){
fprintf(file,"%d\n",ptr->key);ptr=ptr->next;

}

close(fp);
return 1;

}

随手写的,应该没错吧!自己调试下!