|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct friends{
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
- struct friends *next;
- };
- int size=sizeof(struct friends);
- void main()
- {
- struct friends *create_link_table(struct friends *head,int x);
- struct friends *edit_node(struct friends *head,char name1[]);
- struct friends *query_link_table(struct friends *head);
- struct friends *head=NULL,*p=NULL;
- int s,x;
- int n;
- char name1[50];
- int sel;
- while(1)
- {
- system("cls");
- printf("========================================\n");
- printf("|---1.添加朋友信息----2.修改朋友信息---|\n");
- printf("|---3.查询朋友信息----4.删除朋友信息---|\n");
- printf("|---5.输出朋友信息----0.退出-----------|\n");
- printf("========================================\n");
- printf("请选择输入(1-5),输入0退出\n");
- scanf("%d",&sel);
- switch(sel)
- {
- case 1: //输入朋友信息
- printf("请输入添加朋友信息个数\n");
- scanf("%d",&x);
- head=create_link_table(head,x);
- printf("\n按任意键继续");
- getchar();
- break;
- case 2: //修改朋友信息
- printf("请输入需要修改信息的朋友名字:\n");
- scanf("%s",name1);
- head=edit_node(head,name1);
- getchar();
- break;
- case 3:
- head=query_link_table(head);
- getchar();
- break;
- }
- system("pause");
- }
- }
- struct friends *create_link_table(struct friends *head,int x)
- {
- FILE *fp;
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
- int n=0,s;
- struct friends *p1=NULL,*p2=NULL;
- if((fp=fopen("朋友圈信息.txt","ab"))==NULL)//写入文本
- {
- printf("Can not open the file\n");
- exit(0);
- }
-
- printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
- while(n<x)
- {
- scanf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- n++;
- p1=(struct friends *)malloc(size);
- strcpy(p1->name,name);
- strcpy(p1->sex,sex);
- strcpy(p1->telephone,telephone);
- strcpy(p1->address,address);
- strcpy(p1->wechat,wechat);
- strcpy(p1->QQ,QQ);
- p1->next=NULL;
- if(n==1)
- head=p1;
- else
- p2->next=p1;
- p2=p1;
- fwrite(p1,size,1,fp);
- }
- fclose(fp);
- return head;
- }
- struct friends *query_link_table(struct friends *head)
- {
- FILE *fp;
- int n=0;
- struct friends *p1=NULL,*p2=NULL,*list=NULL;
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
- if((fp=fopen("朋友圈信息.txt","rb"))==NULL)
- {
- printf("can not open the file\n");
- exit(0);
- }
-
- printf("姓名 性别 电话号码 地址 微信 QQ号码");
- printf("\n");
- list=head;
- while(!feof(fp))
- {
-
-
- fread(head,size,1,fp);
- printf("%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
- //printf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- printf("\n");
- head=head->next;
- }
- fclose(fp);
- return list;
- }
- 为什么运行不了 求解,我现在是想用链表把往文件里面放东西,然后再用另一个函数把文件里的内容用链表方式读出来 求解为什么不可以
复制代码 |
|