C语言如何将链表里的值写入文件

Python011

C语言如何将链表里的值写入文件,第1张

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct datanode {

char name[24]

char phone[12]

// ......

struct datanode *next

}*pNode,*LinkList,Node

LinkList getEmptyList() {

LinkList head = (pNode)malloc(sizeof(Node))

head->next = NULL

return head

}

void addNode(LinkList head, pNode pnode) {

pnode->next = head->next

head->next = pnode

}

void writeFile(LinkList head) {

FILE *outf

pNode p = head->next

if((outf = fopen("data.txt","wt")) == NULL) {

printf("不能打开数据文件。\n")

exit(1)

}

while(p) {

fwrite(p,sizeof(Node),1,outf)

p = p->next

}

fclose(outf)

}

int main() {

char name[24],phone[12]

// ......

pNode p

LinkList head = getEmptyList()

printf("姓名 联系电话\n")

while(scanf("%s%s",name,phone) == 2) {

p = (pNode)malloc(sizeof(Node))

strcpy(p->name,name)

strcpy(p->phone,phone)

addNode(head,p)

printf("姓名 联系电话(<Ctrl + Z> <ENTER> 结束)\n")

}

writeFile(head)

return 0

}

只需要将文件标示为二进制即可。\x0d\x0astruct student stu[256]\x0d\x0a//将stu赋值...\x0d\x0a\x0d\x0aFILE * fd=fopen("c:\\test.bin","wb")//打开\x0d\x0aint i\x0d\x0afor(i=0i 回答于 2022-11-16