鱼C论坛

 找回密码
 立即注册
查看: 2373|回复: 2

[已解决]C语言文件和链表之间的运用求帮忙

[复制链接]
发表于 2015-12-19 00:02:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct friends{
  5.        
  6.         char name[50];  //姓名
  7.         char sex[10];   //性别
  8.         char telephone[15];  //电话号码
  9.         char address[50];   //地址
  10.         char wechat[50];    //微信
  11.         char QQ[15];         //QQ号码
  12.         struct friends *next;
  13. };
  14. int size=sizeof(struct friends);
  15. void main()
  16. {
  17.         struct friends *create_link_table(struct friends *head,int x);
  18.         struct friends *edit_node(struct friends *head,char name1[]);
  19.         void query_link_table();
  20.         struct friends *head=NULL,*p=NULL;
  21.         int s,x;
  22.         int n;
  23.         char name1[50];
  24.        
  25.         int sel;
  26.        
  27.         while(1)
  28.         {  
  29.                
  30.                 system("cls");
  31.                 printf("========================================\n");
  32.                 printf("|---1.添加朋友信息----2.修改朋友信息---|\n");
  33.                 printf("|---3.查询朋友信息----4.删除朋友信息---|\n");
  34.                 printf("|---5.输出朋友信息----0.退出-----------|\n");
  35.                 printf("========================================\n");
  36.                 printf("请选择输入(1-5),输入0退出\n");
  37.                 scanf("%d",&sel);
  38.                 switch(sel)
  39.                 {
  40.                 case 1: //输入朋友信息
  41.                         printf("请输入添加朋友信息个数\n");
  42.                         scanf("%d",&x);
  43.                         head=create_link_table(head,x);
  44.                         printf("\n按任意键继续");
  45.                         getchar();
  46.                         break;
  47.                        
  48.                 case 2: //修改朋友信息
  49.                         printf("请输入需要修改信息的朋友名字:\n");
  50.                         scanf("%s",name1);
  51.                         head=edit_node(head,name1);
  52.                         getchar();
  53.                         break;
  54.                        
  55.                 case 3:
  56.                         query_link_table();
  57.                         getchar();
  58.                         break;
  59.                         }
  60.                 system("pause");
  61.                
  62.         }
  63.        
  64. }
  65. struct friends *create_link_table(struct friends *head,int x)
  66. {  
  67.         FILE *fp;
  68.         char name[50];  //姓名
  69.     char sex[10];   //性别
  70.     char telephone[15];  //电话号码
  71.     char address[50];   //地址
  72.     char wechat[50];    //微信
  73.     char QQ[15];  //QQ号码
  74.         int n=0,s;
  75.         struct friends *p1=NULL,*p2=NULL;
  76.         if((fp=fopen("朋友圈信息.txt","a"))==NULL)//写入文本
  77.         {
  78.                 printf("Can not open the file\n");
  79.                 exit(0);
  80.         }
  81.    
  82.        
  83.     printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
  84.        
  85.         head=NULL;
  86.         while(n<x)
  87.         {   
  88.                 scanf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  89.                 n++;
  90.                 p1=(struct friends *)malloc(size);
  91.                 strcpy(p1->name,name);
  92.                 strcpy(p1->sex,sex);
  93.                 strcpy(p1->telephone,telephone);
  94.                 strcpy(p1->address,address);
  95.                 strcpy(p1->wechat,wechat);
  96.                 strcpy(p1->QQ,QQ);
  97.                 p1->next=NULL;
  98.                 if(n==1)
  99.                         head=p1;
  100.                 else
  101.                         p2->next=p1;
  102.                 p2=p1;
  103.                
  104.                 fprintf(fp,"%-6s%6s%13s%10s%12s%12s",p1->name,p1->sex,p1->telephone,p1->address,p1->wechat,p1->QQ);
  105.                 fprintf(fp,"\n");
  106.        
  107.         }
  108.        
  109.         fclose(fp);
  110.         return head;
  111.        
  112. }
  113. void query_link_table()
  114. {
  115.         FILE *fp;
  116.         char name[50];  //姓名
  117.     char sex[10];   //性别
  118.     char telephone[15];  //电话号码
  119.     char address[50];   //地址
  120.     char wechat[50];    //微信
  121.     char QQ[15];  //QQ号码
  122.        
  123.         if((fp=fopen("朋友圈信息.txt","r"))==NULL)
  124.         {
  125.                 printf("can not open the file\n");
  126.                 exit(0);
  127.         }
  128.           
  129.        
  130.         printf("姓名     性别     电话号码    地址       微信     QQ号码");
  131.         printf("\n");
  132.         while(!feof(fp))
  133.         {   
  134.                 fscanf(fp,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  135.                 printf("%-6s%6s%12s%10s%12s%12s",name,sex,telephone,address,wechat,QQ);
  136.                 printf("\n");
  137.         }
  138.         fclose(fp);
  139.        
  140. }

  141. struct friends *edit_node(struct friends *head,char name1[])
  142. {
  143.        
  144.         char name[50];  //姓名
  145.     char sex[10];   //性别
  146.     char telephone[15];  //电话号码
  147.     char address[50];   //地址
  148.     char wechat[50];    //微信
  149.     char QQ[15];  //QQ号码
  150.         FILE *f;
  151.        
  152.         int n=0;
  153.        
  154.         if((f=fopen("朋友圈信息.txt","r"))==NULL)
  155.         {
  156.                 printf("can not open the file");
  157.                 exit(0);
  158.         }
  159.            while(!feof(f))
  160.         {       
  161.                 fscanf(f,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  162.         }
  163.         fclose(f);
  164.        
  165.            if((f=fopen("朋友圈信息.txt","w"))==NULL)
  166.         {       
  167.                 printf("can not open the file");
  168.                 exit(0);
  169.         }
  170.        
  171.     while(head!=NULL)
  172.         {
  173.                 if((strcmp(name1,head->name)!=0))
  174.                 {       
  175.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
  176.                         fprintf(f,"\n");
  177.                        
  178.                 }
  179.                 else
  180.                 {  
  181.                         printf("重新写入地址\n");
  182.                         scanf("%s",head->address);
  183.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
  184.                         fprintf(f,"\n");
  185.             
  186.                 }
  187.                
  188.                 head=head->next;
  189.         }
  190.         fclose(f);
  191.         return head;
  192. }
复制代码




第二个修改功能只能用一次,求解为什么 还有链表与文件之间的关系  求解 不好意思 代码有点长  请大神们细心看,我是认真想学好C语言上来求助的,自己做了好几天没想到 细心:cry
最佳答案
2015-12-26 09:50:37
之所以只能用一次是因为第一次你把head指向NULL了,第二次进入时已经是NULL,所以不能再修改。
我改以一下,你看一下。还有就是你的txt每次进入都是w格式,那之前储存的信息不就没了?
and就是好像你的功能三有点问题,你试一下吧,我只是简单弄了一下,没仔细看。 最近忙疯了,不好意思啊
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct friends
  5. {
  6.         
  7.         char name[50];  //姓名
  8.         char sex[10];   //性别
  9.         char telephone[15];  //电话号码
  10.         char address[50];   //地址
  11.         char wechat[50];    //微信
  12.         char QQ[15];         //QQ号码
  13.         struct friends *next;
  14. };
  15. int size=sizeof(struct friends);
  16. void main()
  17. {
  18.         struct friends *create_link_table(struct friends *head,int x);
  19.         struct friends *edit_node(struct friends *head,char name1[]);
  20.         void query_link_table();
  21.         struct friends *head=NULL,*p=NULL;
  22.         int s,x;
  23.         int n;
  24.         char name1[50];
  25.         
  26.         int sel;
  27.         
  28.         while(1)
  29.         {  
  30.                
  31.                 system("cls");
  32.                 printf("========================================\n");
  33.                 printf("|---1.添加朋友信息----2.修改朋友信息---|\n");
  34.                 printf("|---3.查询朋友信息----4.删除朋友信息---|\n");
  35.                 printf("|---5.输出朋友信息----0.退出-----------|\n");
  36.                 printf("========================================\n");
  37.                 printf("请选择输入(1-5),输入0退出\n");
  38.                 scanf("%d",&sel);
  39.                 switch(sel)
  40.                 {
  41.                 case 1: //输入朋友信息
  42.                         printf("请输入添加朋友信息个数\n");
  43.                         scanf("%d",&x);
  44.                         head=create_link_table(head,x);
  45.                         printf("\n按任意键继续");
  46.                         getchar();
  47.                         break;
  48.                         
  49.                 case 2: //修改朋友信息
  50.                         printf("请输入需要修改信息的朋友名字:\n");
  51.                         scanf("%s",name1);
  52.                         head=edit_node(head,name1);
  53.                         getchar();
  54.                         break;
  55.                         
  56.                 case 3:
  57.                         query_link_table();
  58.                         getchar();
  59.                         break;
  60.                         }
  61.                 system("pause");
  62.                
  63.         }
  64.         
  65. }
  66. struct friends *create_link_table(struct friends *head,int x)
  67. {  
  68.         FILE *fp;
  69.         char name[50];  //姓名
  70.     char sex[10];   //性别
  71.     char telephone[15];  //电话号码
  72.     char address[50];   //地址
  73.     char wechat[50];    //微信
  74.     char QQ[15];  //QQ号码
  75.         int n=0,s;
  76.         struct friends *p1=NULL,*p2=NULL;
  77.         if((fp=fopen("朋友圈信息.txt","a"))==NULL)//写入文本
  78.         {
  79.                 printf("Can not open the file\n");
  80.                 exit(0);
  81.         }
  82.    
  83.         
  84.     printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
  85.         
  86.         head=NULL;
  87.         while(n<x)
  88.         {   
  89.                 scanf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  90.                 n++;
  91.                 p1=(struct friends *)malloc(size);
  92.                 strcpy(p1->name,name);
  93.                 strcpy(p1->sex,sex);
  94.                 strcpy(p1->telephone,telephone);
  95.                 strcpy(p1->address,address);
  96.                 strcpy(p1->wechat,wechat);
  97.                 strcpy(p1->QQ,QQ);
  98.                 p1->next=NULL;
  99.                 if(n==1)
  100.                         head=p1;
  101.                 else
  102.                         p2->next=p1;
  103.                 p2=p1;
  104.                
  105.                 fprintf(fp,"%-6s%6s%13s%10s%12s%12s",p1->name,p1->sex,p1->telephone,p1->address,p1->wechat,p1->QQ);
  106.                 fprintf(fp,"\n");
  107.         
  108.         }
  109.         
  110.         fclose(fp);
  111.         return head;
  112.         
  113. }
  114. void query_link_table()
  115. {
  116.     FILE *fp;
  117.     char name[50];  //姓名
  118.     char sex[10];   //性别
  119.     char telephone[15];  //电话号码
  120.     char address[50];   //地址
  121.     char wechat[50];    //微信
  122.     char QQ[15];  //QQ号码
  123.         
  124.         if((fp=fopen("朋友圈信息.txt","r"))==NULL)
  125.         {
  126.                 printf("can not open the file\n");
  127.                 exit(0);
  128.         }
  129.            
  130.         
  131.         printf("姓名     性别     电话号码    地址       微信     QQ号码");
  132.         printf("\n");
  133.         while(!feof(fp))
  134.         {   
  135.                 fscanf(fp,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  136.                 printf("%-6s%6s%12s%10s%12s%12s",name,sex,telephone,address,wechat,QQ);
  137.                 printf("\n");
  138.         }
  139.         fclose(fp);
  140.         
  141. }

  142. struct friends *edit_node(struct friends *head,char name1[])
  143. {
  144.         
  145.     char name[50];  //姓名
  146.     char sex[10];   //性别
  147.     char telephone[15];  //电话号码
  148.     char address[50];   //地址
  149.     char wechat[50];    //微信
  150.     char QQ[15];  //QQ号码
  151.         struct friends *p=head;
  152.         FILE *f;
  153.         
  154.         int n=0;
  155.         
  156.         if((f=fopen("朋友圈信息.txt","r"))==NULL)
  157.         {
  158.                 printf("can not open the file");
  159.                 exit(0);
  160.         }
  161.            while(!feof(f))
  162.         {        
  163.                 fscanf(f,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  164.         }
  165.         fclose(f);
  166.         
  167.            if((f=fopen("朋友圈信息.txt","w"))==NULL)
  168.         {        
  169.                 printf("can not open the file");
  170.                 exit(0);
  171.         }
  172.         
  173.     while(p!=NULL)
  174.         {
  175.                 if((strcmp(name1,p->name)!=0))
  176.                 {        
  177.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
  178.                         fprintf(f,"\n");
  179.                         
  180.                 }
  181.                 else
  182.                 {  
  183.                         printf("重新写入地址\n");
  184.                         scanf("%s",head->address);
  185.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
  186.                         fprintf(f,"\n");
  187.             
  188.                 }
  189.                
  190.                 p=p->next;
  191.         }
  192.         fclose(f);
  193.         return head;
  194. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-25 21:01:15 | 显示全部楼层
本帖最后由 q312102408 于 2015-12-25 21:06 编辑

123
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-26 09:50:37 | 显示全部楼层    本楼为最佳答案   
之所以只能用一次是因为第一次你把head指向NULL了,第二次进入时已经是NULL,所以不能再修改。
我改以一下,你看一下。还有就是你的txt每次进入都是w格式,那之前储存的信息不就没了?
and就是好像你的功能三有点问题,你试一下吧,我只是简单弄了一下,没仔细看。 最近忙疯了,不好意思啊
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct friends
  5. {
  6.         
  7.         char name[50];  //姓名
  8.         char sex[10];   //性别
  9.         char telephone[15];  //电话号码
  10.         char address[50];   //地址
  11.         char wechat[50];    //微信
  12.         char QQ[15];         //QQ号码
  13.         struct friends *next;
  14. };
  15. int size=sizeof(struct friends);
  16. void main()
  17. {
  18.         struct friends *create_link_table(struct friends *head,int x);
  19.         struct friends *edit_node(struct friends *head,char name1[]);
  20.         void query_link_table();
  21.         struct friends *head=NULL,*p=NULL;
  22.         int s,x;
  23.         int n;
  24.         char name1[50];
  25.         
  26.         int sel;
  27.         
  28.         while(1)
  29.         {  
  30.                
  31.                 system("cls");
  32.                 printf("========================================\n");
  33.                 printf("|---1.添加朋友信息----2.修改朋友信息---|\n");
  34.                 printf("|---3.查询朋友信息----4.删除朋友信息---|\n");
  35.                 printf("|---5.输出朋友信息----0.退出-----------|\n");
  36.                 printf("========================================\n");
  37.                 printf("请选择输入(1-5),输入0退出\n");
  38.                 scanf("%d",&sel);
  39.                 switch(sel)
  40.                 {
  41.                 case 1: //输入朋友信息
  42.                         printf("请输入添加朋友信息个数\n");
  43.                         scanf("%d",&x);
  44.                         head=create_link_table(head,x);
  45.                         printf("\n按任意键继续");
  46.                         getchar();
  47.                         break;
  48.                         
  49.                 case 2: //修改朋友信息
  50.                         printf("请输入需要修改信息的朋友名字:\n");
  51.                         scanf("%s",name1);
  52.                         head=edit_node(head,name1);
  53.                         getchar();
  54.                         break;
  55.                         
  56.                 case 3:
  57.                         query_link_table();
  58.                         getchar();
  59.                         break;
  60.                         }
  61.                 system("pause");
  62.                
  63.         }
  64.         
  65. }
  66. struct friends *create_link_table(struct friends *head,int x)
  67. {  
  68.         FILE *fp;
  69.         char name[50];  //姓名
  70.     char sex[10];   //性别
  71.     char telephone[15];  //电话号码
  72.     char address[50];   //地址
  73.     char wechat[50];    //微信
  74.     char QQ[15];  //QQ号码
  75.         int n=0,s;
  76.         struct friends *p1=NULL,*p2=NULL;
  77.         if((fp=fopen("朋友圈信息.txt","a"))==NULL)//写入文本
  78.         {
  79.                 printf("Can not open the file\n");
  80.                 exit(0);
  81.         }
  82.    
  83.         
  84.     printf("请输入朋友信息(姓名,性别,电话号码,地址,微信,QQ号码)\n");
  85.         
  86.         head=NULL;
  87.         while(n<x)
  88.         {   
  89.                 scanf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  90.                 n++;
  91.                 p1=(struct friends *)malloc(size);
  92.                 strcpy(p1->name,name);
  93.                 strcpy(p1->sex,sex);
  94.                 strcpy(p1->telephone,telephone);
  95.                 strcpy(p1->address,address);
  96.                 strcpy(p1->wechat,wechat);
  97.                 strcpy(p1->QQ,QQ);
  98.                 p1->next=NULL;
  99.                 if(n==1)
  100.                         head=p1;
  101.                 else
  102.                         p2->next=p1;
  103.                 p2=p1;
  104.                
  105.                 fprintf(fp,"%-6s%6s%13s%10s%12s%12s",p1->name,p1->sex,p1->telephone,p1->address,p1->wechat,p1->QQ);
  106.                 fprintf(fp,"\n");
  107.         
  108.         }
  109.         
  110.         fclose(fp);
  111.         return head;
  112.         
  113. }
  114. void query_link_table()
  115. {
  116.     FILE *fp;
  117.     char name[50];  //姓名
  118.     char sex[10];   //性别
  119.     char telephone[15];  //电话号码
  120.     char address[50];   //地址
  121.     char wechat[50];    //微信
  122.     char QQ[15];  //QQ号码
  123.         
  124.         if((fp=fopen("朋友圈信息.txt","r"))==NULL)
  125.         {
  126.                 printf("can not open the file\n");
  127.                 exit(0);
  128.         }
  129.            
  130.         
  131.         printf("姓名     性别     电话号码    地址       微信     QQ号码");
  132.         printf("\n");
  133.         while(!feof(fp))
  134.         {   
  135.                 fscanf(fp,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  136.                 printf("%-6s%6s%12s%10s%12s%12s",name,sex,telephone,address,wechat,QQ);
  137.                 printf("\n");
  138.         }
  139.         fclose(fp);
  140.         
  141. }

  142. struct friends *edit_node(struct friends *head,char name1[])
  143. {
  144.         
  145.     char name[50];  //姓名
  146.     char sex[10];   //性别
  147.     char telephone[15];  //电话号码
  148.     char address[50];   //地址
  149.     char wechat[50];    //微信
  150.     char QQ[15];  //QQ号码
  151.         struct friends *p=head;
  152.         FILE *f;
  153.         
  154.         int n=0;
  155.         
  156.         if((f=fopen("朋友圈信息.txt","r"))==NULL)
  157.         {
  158.                 printf("can not open the file");
  159.                 exit(0);
  160.         }
  161.            while(!feof(f))
  162.         {        
  163.                 fscanf(f,"%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  164.         }
  165.         fclose(f);
  166.         
  167.            if((f=fopen("朋友圈信息.txt","w"))==NULL)
  168.         {        
  169.                 printf("can not open the file");
  170.                 exit(0);
  171.         }
  172.         
  173.     while(p!=NULL)
  174.         {
  175.                 if((strcmp(name1,p->name)!=0))
  176.                 {        
  177.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
  178.                         fprintf(f,"\n");
  179.                         
  180.                 }
  181.                 else
  182.                 {  
  183.                         printf("重新写入地址\n");
  184.                         scanf("%s",head->address);
  185.                         fprintf(f,"%-6s%6s%13s%10s%12s%12s",p->name,p->sex,p->telephone,p->address,p->wechat,p->QQ);
  186.                         fprintf(f,"\n");
  187.             
  188.                 }
  189.                
  190.                 p=p->next;
  191.         }
  192.         fclose(f);
  193.         return head;
  194. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 16:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表