鱼C论坛

 找回密码
 立即注册
查看: 2284|回复: 14

[已解决]求帮助,非常着急,大佬们,有关链表与文件的

[复制链接]
发表于 2018-9-6 19:58:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-7 13:23:08 | 显示全部楼层
易晓外 发表于 2018-9-6 23:29
666,厉害,PS:不过我想知道我的为什么不可以呢?怎么会多出一组呢?不是到文件尾就结束循环了吗?

*head = NULL
不是很清楚为什么哥哥要这样写
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-7 13:26:16 | 显示全部楼层
本帖最后由 claws0n 于 2018-9-7 13:29 编辑
人造人 发表于 2018-9-6 23:49
自己百度 feof函数


写入
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Student
  5. {
  6.     char name[20];
  7.     long long int numb;
  8.     struct Student* pNext;
  9. } ;

  10. struct Student_1
  11. {
  12.     char name[20];
  13.     long long int numb;
  14. } ;

  15. struct Student * create()
  16. {
  17.     FILE * fp;
  18.     if((fp=fopen("student.txt","wb")) == NULL)
  19.     {
  20.             printf("cannot open file\n");
  21.             exit(0);
  22.     }
  23.    
  24.     struct Student *head, *pNew, *p;
  25.         head->pNext = NULL;
  26.     char ch;
  27.    
  28.     pNew = (struct Student*)malloc(sizeof(struct Student));
  29.    
  30.     p = head;        //索引指针
  31.     while( p->pNext )                //尾插法
  32.         p = p->pNext;
  33.     do
  34.     {
  35.         printf("请输入学生姓名:\n");
  36.         scanf("%s", pNew->name);
  37.         getchar();
  38.         printf("请输入学生学号:\n");
  39.         scanf("%lld", &pNew->numb);
  40.         getchar();
  41.                
  42.         p->pNext = pNew;
  43.         pNew->pNext = NULL;
  44.         p = p->pNext;
  45.         
  46.         printf("是否继续录入(y/n):\n");
  47.         ch = getchar();
  48.         getchar();
  49.         if(ch == 'y'|| ch == 'Y') //把大写也纳入
  50.            pNew = (struct Student*)malloc(sizeof(struct Student));
  51.            
  52.     }while(ch == 'y'|| ch == 'Y'); //把大写也纳入
  53.    
  54.     p = head->pNext;
  55.     while( p )
  56.     {  
  57.         if(fwrite(p, sizeof(struct Student_1), 1, fp) != 1)
  58.         {
  59.             printf("录入出错!\n");
  60.         }
  61.         p = p->pNext;
  62.     }
  63.     fclose(fp);
  64.     return head;
  65. }

  66. void print(struct Student* head)
  67. {
  68.     struct Student *p;
  69.     p = head->pNext;
  70.     printf("姓名\t\t学号\n");
  71.     while( p )
  72.     {
  73.         printf("%s\t\t%lld\n", p->name, p->numb);
  74.         p = p->pNext;
  75.     }
  76. }


  77. int main()
  78. {
  79.     struct Student* head;
  80.     head = create();
  81.     system("cls");
  82.     print(head);
  83.     return 0;
  84. }
复制代码
读取
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Student
  5. {
  6.     char name[20];
  7.     long long int numb;
  8.     struct Student * pNext;
  9. } ;

  10. struct Student_1
  11. {
  12.     char name[20];
  13.     long long int numb;
  14. } ;


  15. struct Student * fprint()
  16. {
  17. FILE *fp;
  18. struct Student *head, *pNew, *p;
  19.        
  20. if((fp = fopen("student.txt","rb")) == NULL)
  21. {
  22.         printf("不能打开文件!");
  23.         exit(0);
  24. }
  25.        
  26. head->pNext = NULL;
  27. p = head;
  28.        
  29. while(!feof(fp))
  30. {
  31.         pNew = (struct Student*)malloc(sizeof(struct Student));
  32.         fread(pNew, sizeof(struct Student_1), 1, fp);
  33.                
  34.         p->pNext = pNew;
  35.         pNew->pNext = NULL;
  36.         p = p->pNext;
  37. }
  38. return head;
  39. }



  40. void print(struct Student* head)
  41. {
  42.     struct Student *p;
  43.     p = head->pNext;
  44.     printf("姓名\t\t学号\n");
  45.     while( p->pNext )
  46.     {
  47.         printf("%s\t\t%lld\n", p->name, p->numb);
  48.         p = p->pNext;
  49.     }
  50. }

  51. int main()
  52. {
  53.     struct Student* head;
  54.     head = fprint();
  55.     printf("reading...\n");
  56.     print(head);
  57.     return 0;
  58. }
复制代码

*head = NULL 的问题吧?虽然浪费了一个空间,但这样写不是更好吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-7 18:30:41 | 显示全部楼层

问题应该是出至于头指针。我把插入的方法稍微改了一下,就不会有问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-5 04:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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