鱼C论坛

 找回密码
 立即注册
查看: 1758|回复: 0

[技术交流] 交流链表与文件

[复制链接]
发表于 2015-12-20 12:28:42 | 显示全部楼层 |阅读模式

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

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

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


  120.          fread(head,size,1,fp);
  121.        printf("%-6s%6s%13s%10s%12s%12s",head->name,head->sex,head->telephone,head->address,head->wechat,head->QQ);
  122. //printf("%s%s%s%s%s%s",name,sex,telephone,address,wechat,QQ);
  123.   printf("\n");
  124.         head=head->next;

  125. }
  126. fclose(fp);
  127. return list;
  128. }
  129. 为什么运行不了 求解,我现在是想用链表把往文件里面放东西,然后再用另一个函数把文件里的内容用链表方式读出来  求解为什么不可以
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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