#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct shebei
{ int xuhao
char bianhao[40]
char didian[40]
char mingcheng[40]
char zhuangtai[40]
struct shebei*next
}SHEBEI
int n
void print(struct shebei*head)
struct shebei*creat()
void RW_File(SHEBEI *head)
void main()
{ struct shebei*head
head=creat()
RW_File(head)
printf("\nji xu jia you o !!!\n")
}
struct shebei*creat()
{ struct shebei*p1,*p2
struct shebei*head
n=0
p1=p2=(struct shebei*)malloc(sizeof(struct shebei))
head=NULL
printf("shu ru xu hao:\n")
scanf("%d",&p1->xuhao)
printf("shu xu bian hao:\n")
scanf("%s",p1->bianhao)
printf("shu ru di dian:\n")
scanf("%s",p1->didian)
printf("shu ru ming cheng:\n")
scanf("%s",p1->mingcheng)
printf("shu ru zhuang tai:\n")
scanf("%s",p1->zhuangtai)
while(p1->xuhao!=0)
{ n=n+1
if(n==1)
head=p1
else
p2->next=p1
p2=p1
p1=(struct shebei*)malloc(sizeof(struct shebei))
printf("shu ru xu hao:\n")
scanf("%d",&p1->xuhao)
printf("shu xu bian hao:\n")
scanf("%s",p1->bianhao)
printf("shu ru di dian:\n")
scanf("%s",p1->didian)
printf("shu ru ming cheng:\n")
scanf("%s",p1->mingcheng)
printf("shu ru zhuang tai:\n")
scanf("%s",p1->zhuangtai)
}
p2->next=NULL
return(head)
}
void RW_File(SHEBEI *head)/*head是你刚刚创建的链表的头指针*/
{
SHEBEI SingleInfo[20]
FILE *fp
int i=0
if((fp=fopen("d:\\shebei","wb+"))==NULL)
{
printf("can not open file")
exit(1)
}
while(head)
{
fwrite(head,sizeof(SHEBEI),1,fp)
head = head->next
}
rewind(fp)/*到这里已经把链表写入到d盘的shebei文件中*/
while(!feof(fp))
{
fread(&SingleInfo[i],sizeof(SHEBEI),1,fp)
i++
}
fclose(fp)
}
只需要将文件标示为二进制即可。\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#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
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)