|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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[]);
- void query_link_table();
- 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:
- query_link_table();
- 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","a"))==NULL)//写入文本
- {
- printf("Can not open the file\n");
- exit(0);
- }
-
-
- printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
-
- head=NULL;
- 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;
-
- fprintf(fp,"%-6s%6s%13s%10s%12s%12s",p1->name,p1->sex,p1->telephone,p1->address,p1->wechat,p1->QQ);
- fprintf(fp,"\n");
-
- }
-
- fclose(fp);
- return head;
-
- }
- void query_link_table()
- {
- FILE *fp;
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
-
- if((fp=fopen("朋友圈信息.txt","r"))==NULL)
- {
- printf("can not open the file\n");
- exit(0);
- }
-
-
- printf("姓名 性别 电话号码 地址 微信 QQ号码");
- printf("\n");
- while(!feof(fp))
- {
- fscanf(fp,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- printf("%-6s%6s%12s%10s%12s%12s",name,sex,telephone,address,wechat,QQ);
- printf("\n");
- }
- fclose(fp);
-
- }
- struct friends *edit_node(struct friends *head,char name1[])
- {
-
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
- FILE *f;
-
- int n=0;
-
- if((f=fopen("朋友圈信息.txt","r"))==NULL)
- {
- printf("can not open the file");
- exit(0);
- }
- while(!feof(f))
- {
- fscanf(f,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- }
- fclose(f);
-
- if((f=fopen("朋友圈信息.txt","w"))==NULL)
- {
- printf("can not open the file");
- exit(0);
- }
-
- while(head!=NULL)
- {
- if((strcmp(name1,head->name)!=0))
- {
- fprintf(f,"%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
- fprintf(f,"\n");
-
- }
- else
- {
- printf("重新写入地址\n");
- scanf("%s",head->address);
- fprintf(f,"%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
- fprintf(f,"\n");
-
- }
-
- head=head->next;
- }
- fclose(f);
- return head;
- }
复制代码
第二个修改功能只能用一次,求解为什么 还有链表与文件之间的关系 求解 不好意思 代码有点长 请大神们细心看,我是认真想学好C语言上来求助的,自己做了好几天没想到 细心:cry
之所以只能用一次是因为第一次你把head指向NULL了,第二次进入时已经是NULL,所以不能再修改。
我改以一下,你看一下。还有就是你的txt每次进入都是w格式,那之前储存的信息不就没了?
and就是好像你的功能三有点问题,你试一下吧,我只是简单弄了一下,没仔细看。  最近忙疯了,不好意思啊 - #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[]);
- void query_link_table();
- 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:
- query_link_table();
- 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","a"))==NULL)//写入文本
- {
- printf("Can not open the file\n");
- exit(0);
- }
-
-
- printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
-
- head=NULL;
- 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;
-
- fprintf(fp,"%-6s%6s%13s%10s%12s%12s",p1->name,p1->sex,p1->telephone,p1->address,p1->wechat,p1->QQ);
- fprintf(fp,"\n");
-
- }
-
- fclose(fp);
- return head;
-
- }
- void query_link_table()
- {
- FILE *fp;
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
-
- if((fp=fopen("朋友圈信息.txt","r"))==NULL)
- {
- printf("can not open the file\n");
- exit(0);
- }
-
-
- printf("姓名 性别 电话号码 地址 微信 QQ号码");
- printf("\n");
- while(!feof(fp))
- {
- fscanf(fp,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- printf("%-6s%6s%12s%10s%12s%12s",name,sex,telephone,address,wechat,QQ);
- printf("\n");
- }
- fclose(fp);
-
- }
- struct friends *edit_node(struct friends *head,char name1[])
- {
-
- char name[50]; //姓名
- char sex[10]; //性别
- char telephone[15]; //电话号码
- char address[50]; //地址
- char wechat[50]; //微信
- char QQ[15]; //QQ号码
- struct friends *p=head;
- FILE *f;
-
- int n=0;
-
- if((f=fopen("朋友圈信息.txt","r"))==NULL)
- {
- printf("can not open the file");
- exit(0);
- }
- while(!feof(f))
- {
- fscanf(f,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
- }
- fclose(f);
-
- if((f=fopen("朋友圈信息.txt","w"))==NULL)
- {
- printf("can not open the file");
- exit(0);
- }
-
- while(p!=NULL)
- {
- if((strcmp(name1,p->name)!=0))
- {
- fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
- fprintf(f,"\n");
-
- }
- else
- {
- printf("重新写入地址\n");
- scanf("%s",head->address);
- fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
- fprintf(f,"\n");
-
- }
-
- p=p->next;
- }
- fclose(f);
- return head;
- }
复制代码
|
|