鱼C论坛

 找回密码
 立即注册
查看: 2148|回复: 4

[已解决]这个,我刚刚写的代码。功能都正常,但是文件里有东西就奔溃

[复制链接]
发表于 2022-12-9 22:18:13 | 显示全部楼层 |阅读模式
50鱼币
这个是奔溃的样子
FMXB45@GCQUEVCKF@`FCH4L.png
文件是这样的 [GSE}7XAVK%B)@P%I]8$}@P.png
清空就可以打开,
功能都可以正常运行 @R2F%9G2M_Y57P2FF}W}LO9.png 但是,文件里面有东西的话,一运行就不行
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define MAX 1024

  5. struct Book *book_pool = NULL;//内存池
  6. int count_book = 0;
  7. struct User *user_pool = NULL;
  8. int count_user = 0;

  9. struct Date{
  10.         int year;
  11.         int month;
  12.         int day;
  13. };

  14. struct Book{
  15.         char name[40];//书名
  16.         char author[128];//作者
  17.         char publisher[40];//出版社
  18.         int exist;
  19.         struct Date date;//日期
  20.         struct Book *next;
  21. };

  22. struct User{
  23.         char name[40];//用户名
  24.         char cypher[32];//密码
  25.         struct Date date;//注册日期
  26.         struct User *next;
  27. };

  28. void welcome(struct Book **book, struct User **user);//打印欢迎和使用说明
  29. void consumer(void);//打印用户使用说明
  30. void manage(void);//打印管理员使用说明
  31. int log_on(struct User *head);//登录
  32. int administrator(void);//登录管理员
  33. //用户相关的函数
  34. struct User *seek_user(struct User *head);//寻找用户,返回地址
  35. void print_user_one(struct User *head);//根据地址打印用户的信息
  36. void increase(struct User **head);//增加用户
  37. void reduce(struct User **head);//减少用户
  38. void query(struct User *book);//查询用户信息
  39. void modify(struct User *head);//修改用户信息
  40. void display(struct User *head);//显示所有用户信息
  41. //图书相关的函数
  42. struct Book *seek_book(struct Book *head);//寻找图书,返回地址
  43. void print_book_one(struct Book *head);//根据地址打印图书的信息
  44. void insert(struct Book **head);//录入新的图书
  45. void searchEvent(struct Book *book);//查找已有的图书
  46. void change(struct Book **head);//更改已有的图书
  47. void delete(struct Book **head);//删除已有的图书
  48. void print(struct Book *head);//打印所有的图书信息
  49. void enter(struct Book *book, struct User *user);//结束程序时将信息放入文件
  50. void borrow(void){//借书/
  51. }

  52. void still(void){//还书/
  53. }

  54. void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
  55.         FILE *fp1, *fp2;
  56.         struct Book *temp_book = NULL, *book = *head_book;
  57.         struct User *temp_user = NULL, *user = *head_user;
  58.         int i;

  59.         printf("| 欢迎使用图书馆程序 |\n");
  60.         printf("|----- 1:注册 -----|\n");
  61.         printf("|----- 2:登录 -----|\n");
  62.         printf("|----- 3:退出 -----|\n");
  63.         printf("|--------------------|\n");

  64.         if ((fp1 = fopen("book.txt", "r")) == NULL){//fopen获取文件指针
  65.                 printf("打开文件失败!\n");
  66.                 exit(EXIT_FAILURE);
  67.         }
  68.         if ((fp2 = fopen("user.txt", "r")) == NULL){//fopen获取文件指针
  69.                 printf("打开文件失败!\n");
  70.                 exit(EXIT_FAILURE);
  71.         }
  72.         printf("aaa");
  73.         i = 0;
  74.         while (1){
  75.                 fread(temp_book, sizeof(struct Book), 1, fp1);// 将文件数据读取出来
  76.                 if (feof(fp1)){
  77.                         if (!i){
  78.                                 break;
  79.                         }
  80.                         else{
  81.                                 user = temp_user;
  82.                                 user = user->next;
  83.                                 i++;
  84.                                 break;
  85.                         }
  86.                 }
  87.                 book = temp_book;
  88.                 book = book->next;
  89.                 i++;
  90.         }
  91.         i = 0;
  92.         while (1){
  93.                 fread(temp_user, sizeof(struct User), 1, fp2);// 将文件数据读取出来
  94.                 if (feof(fp2)){
  95.                         if (!i){
  96.                                 break;
  97.                         }
  98.                         else{
  99.                                 user = temp_user;
  100.                                 user = user->next;
  101.                                 i++;
  102.                                 break;
  103.                         }
  104.                 }
  105.                 user = temp_user;
  106.                 user = user->next;
  107.                 i++;
  108.         }

  109.         fclose(fp1);//关闭文件!!!!
  110.         fclose(fp2);
  111. }

  112. void consumer(void){//打印用户使用说明
  113.         printf("| 欢迎使用图书馆参观程序 |\n");
  114.         printf("|--- 1:查找已有图书 -----|\n");
  115.         printf("|--- 2:显示当前图书库 ---|\n");
  116.         printf("|--- 3:进入管理员模式 ---|\n");
  117.         printf("|--- 4:退出图书馆程序 ---|\n");
  118.         printf("|----------------------|\n");
  119. }

  120. void manage(void){//打印管理员使用说明
  121.         printf("| 欢迎使用图书馆管理程序 |\n");
  122.         printf("|--- 1:插入新的图书 -----|\n");
  123.         printf("|--- 2:更改已有图书 -----|\n");
  124.         printf("|--- 3:删除已有图书 -----|\n");
  125.         printf("|--- 4:显示当前图书库 ---|\n");
  126.         printf("|--- 5:更改用户信息 -----|\n");
  127.         printf("|--- 6:查询用户信息 -----|\n");
  128.         printf("|--- 7:删除用户信息 -----|\n");
  129.         printf("|--- 8:显示用户名单 -----|\n");
  130.         printf("|--- 9:退出图书馆程序 ---|\n");
  131.         printf("|----------------------|\n");
  132. }

  133. int log_on(struct User *head){//登录/
  134.         struct User *user;
  135.         char mm[32];
  136.         user = seek_user(head);
  137.         printf("请输入密码:");
  138.         scanf("%s", mm);
  139.         if (!strcmp(user->cypher, mm)){
  140.                 printf("登录成功");
  141.                 return 1;
  142.         }
  143.         printf("登录失败");
  144.         return 0;
  145. }

  146. int administrator(void){
  147.         char mm[32];

  148.         printf("请输入管理员密码:");
  149.         scanf("%s", mm);
  150.         if (!strcmp("123456", mm)){
  151.                 printf("登录成功");
  152.                 return 1;
  153.         }
  154.         printf("登录失败");
  155.         return 0;
  156. }

  157. struct User *seek_user(struct User *head){//寻找用户,返回地址
  158.         struct User *target;
  159.         char name[40];

  160.         printf("请输入用户:");
  161.         scanf("%s", name);

  162.         target = head;
  163.         while (target != NULL && strcmp(target->name, name)){
  164.                 target = target->next;
  165.         }

  166.         return target;
  167. }

  168. void print_user_one(struct User *head){//根据地址打印用户的信息
  169.         printf("密码:%s\n", head->cypher);
  170.         printf("注册日期(aa-bb-cc):%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  171. }

  172. void increase(struct User **head){//增加用户/
  173.         struct User *user;
  174.         struct User *temp;

  175.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  176.                 user = user_pool;
  177.                 user_pool = user_pool->next;
  178.                 count_user--;
  179.         }
  180.         else{//如果内存池空,使用malloc申请内存
  181.                 user = (struct User *)malloc(sizeof(struct User));
  182.                 if(user == NULL){
  183.                         printf("内存分配失败了。\n");
  184.                         exit(1);
  185.                 }
  186.         }
  187.         printf("请输入用户名:");
  188.         scanf("%s", user->name);
  189.         printf("请设置密码:");
  190.         scanf("%s", user->cypher);
  191.         printf("请输入注册日期(aa-bb-cc)");
  192.         scanf("%d-%d-%d", &user->date.year, &user->date.month, &user->date.day);

  193.         if (*head != NULL){
  194.                 temp = *head;
  195.                 *head = user;
  196.                 user->next = temp;
  197.         }
  198.         else{
  199.                 *head = user;
  200.                 user->next = NULL;
  201.         }
  202. }

  203. void reduce(struct User **head){//减少用户/
  204.         struct User *target;//目标
  205.         struct User *now;
  206.         struct User *temp;//上一次

  207.         target = seek_user(*head);// 先找到待删除的节点指针

  208.         if (target == NULL){
  209.                 printf("找不到该用户!\n");
  210.         }
  211.         else{
  212.                 now = *head;
  213.                 temp = NULL;

  214.                 while (now != NULL && now != target){// current定位到待删除的节>点
  215.                         temp = now;
  216.                         now = now->next;
  217.                 }
  218.                 if (temp == NULL){// 待删除的节点是第一个节点
  219.                         *head = now->next;
  220.                 }
  221.                 else{// 待删除的节点不是第一个节点
  222.                         temp->next = now->next;
  223.                 }

  224.                 if (count_user < MAX){//判断内存池有没有空位
  225.                         if (user_pool != NULL){
  226.                                 temp = user_pool;
  227.                                 user_pool = target;
  228.                                 target->next = NULL;
  229.                         }                        
  230.                         else{
  231.                                 user_pool = target;
  232.                                 target->next = NULL;
  233.                         }
  234.                         count_user++;
  235.                 }
  236.                 else{
  237.                         free(target);
  238.                 }
  239.         }
  240. }


  241. void query(struct User *head){//查询用户信息/
  242.         struct User *user;
  243.         char name[32];

  244.         printf("请输入用户名:");
  245.         scanf("%s", name);

  246.         user = head;

  247.         while (user != NULL){
  248.                 if (!strcmp(user->name, name)){
  249.                         break;
  250.                 }
  251.                 user = user->next;
  252.         }
  253.         if(user == NULL){
  254.                 printf("抱歉,没有找到。\n");
  255.         }
  256.         else{
  257.                 print_user_one(user);
  258.         }
  259. }

  260. void modify(struct User *head){//修改用户信息/
  261.         struct User *target;

  262.         char name[32];

  263.         target = seek_user(head);// 先找到待更改的节点指针

  264.         if (target == NULL){
  265.                 printf("找不到该书名!\n");
  266.         }
  267.         else{
  268.                 printf("请设置用户名:");
  269.                 scanf("%s", target->name);
  270.               printf("请设置密码:");
  271.                 scanf("%s", target->cypher);
  272.                 printf("请设置注册日期(aa-bb-cc)");
  273.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  274.         }
  275. }

  276. void display(struct User *head){//显示所有用户信息/
  277.         struct User *now;

  278.         now = head;
  279.         while (now != NULL){
  280.                 printf("用户名:%s\n", now->name);
  281.                 print_user_one(now);
  282.                 now = now->next;
  283.         }
  284. }
  285. //--------------------------图书------------------------------
  286. struct Book *seek_book(struct Book *head){//寻找图书,返回地址
  287.         struct Book *target;
  288.         char name[40];
  289.    
  290.         printf("请输入书名:");
  291.         scanf("%s", name);
  292.    
  293.         target = head;
  294.         while (target != NULL && strcmp(target->name, name)){
  295.                 target = target->next;
  296.         }
  297.    
  298.         return target;
  299. }

  300. void print_one(struct Book *head){//根据地址打印图书的信息
  301.         printf("作者:%s\n", head->author);
  302.         printf("出版社:%s\n", head->publisher);
  303.         printf("日期:%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  304.         printf("书籍状态:");
  305.         switch (head->exist){
  306.                 case 1: printf("可借取\n");break;
  307.                 case 0: printf("不可借取\n");break;
  308.         }
  309. }

  310. void insert(struct Book **head){//录入新的图书
  311.         struct Book *book;
  312.         struct Book *temp;

  313.         if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  314.                 book = book_pool;
  315.                 book_pool = book_pool->next;
  316.                 count_book--;
  317.         }
  318.         else{//如果内存池空,使用malloc申请内存
  319.                 book = (struct Book *)malloc(sizeof(struct Book));
  320.                 if(book == NULL){
  321.                         printf("内存分配失败了。\n");
  322.                         exit(1);
  323.                 }
  324.         }
  325.         printf("请输入书名:");
  326.         scanf("%s", book->name);
  327.         printf("请输入作者:");
  328.         scanf("%s", book->author);
  329.         printf("请输入出版社:");
  330.         scanf("%s", book->publisher);
  331.         printf("请输入日期(aa-bb-cc):");
  332.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  333.         book->exist = 1;

  334.         if (*head != NULL){
  335.                 temp = *head;
  336.                 *head = book;
  337.                 book->next = temp;
  338.         }
  339.         else{
  340.                 *head = book;
  341.                 book->next = NULL;
  342.         }
  343.         putchar('\n');
  344. }


  345. void searchEvent(struct Book *head){//查找已有的书名
  346.         struct Book *book;
  347.         char name[32];

  348.         printf("请输入书名:");
  349.         scanf("%s", name);

  350.         book = head;
  351.        
  352.         while (book != NULL){
  353.                 if (!strcmp(book->name, name)){
  354.                         break;
  355.                 }
  356.                 book = book->next;
  357.         }
  358.         if(book == NULL){
  359.                 printf("抱歉,没有找到。\n");
  360.         }
  361.         else{
  362.                 print_one(book);
  363.         }
  364. }

  365. void change(struct Book **head){//更改
  366.         struct Book *target;

  367.         char name[32];

  368.         target = seek_book(*head);// 先找到待更改的节点指针

  369.         if (target == NULL){
  370.                 printf("找不到该书名!\n");
  371.         }
  372.         else{
  373.                 printf("请输入作者:");
  374.                 scanf("%s", target->author);
  375.                 printf("请输入出版社:");
  376.                 scanf("%s", target->publisher);
  377.                 printf("请输入日期(aa-bb-cc):");
  378.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  379.                 printf("是否可借取(1/0):");
  380.                 scanf("%d", &target->exist);
  381.         }
  382. }

  383. void delete(struct Book **head){//删除
  384.         struct Book *target;//目标
  385.         struct Book *now;
  386.         struct Book *temp;//上一次

  387.         target = seek_book(*head);// 先找到待删除的节点指针

  388.         if (target == NULL){
  389.                 printf("找不到该书名!\n");
  390.         }
  391.         else{
  392.                 now = *head;
  393.                 temp = NULL;
  394.    
  395.                 while (now != NULL && now != target){// current定位到待删除的节点
  396.                         temp = now;
  397.                         now = now->next;
  398.                 }
  399.                 if (temp == NULL){// 待删除的节点是第一个节点
  400.                         *head = now->next;
  401.                 }

  402.                 else{// 待删除的节点不是第一个节点
  403.                         temp->next = now->next;
  404.                 }

  405.                 if (count_book < MAX){//判断内存池有没有空位
  406.                         if (book_pool != NULL){
  407.                                 temp = book_pool;
  408.                                 book_pool = target;
  409.                                 target->next = NULL;
  410.                         }
  411.                         else{
  412.                                 book_pool = target;
  413.                                 target->next = NULL;
  414.                         }
  415.                         count_book++;
  416.                 }
  417.                 else{
  418.                         free(target);
  419.                 }
  420.         }
  421. }

  422. void print(struct Book *head){//打印
  423.         struct Book *now;

  424.         now = head;
  425.         while (now != NULL){
  426.                 printf("书名:%s\n", now->author);
  427.                 print_one(now);
  428.                 now = now->next;
  429.         }
  430. }

  431. void enter(struct Book *book, struct User *user){//结束程序时将图书信息放入文件
  432.         struct Book *book_temp;
  433.         struct User *user_temp;
  434.         FILE *fp1, *fp2;

  435.         if ((fp1 = fopen("book.txt", "wb")) == NULL){//fopen获取文件指针
  436.                 printf("打开文件失败!\n");
  437.                 exit(EXIT_FAILURE);
  438.         }

  439.         if ((fp2 = fopen("user.txt", "wb")) == NULL){//fopen获取文件指针
  440.                 printf("打开文件失败!\n");
  441.                 exit(EXIT_FAILURE);
  442.         }

  443.         while (book != NULL){
  444.                 fwrite(book, sizeof(struct Book), 1, fp1);// 将数据写入到文件中,释放内存
  445.                 book_temp = book;
  446.                 book = book->next;
  447.                 free(book_temp);
  448.         }

  449.         while (user != NULL){
  450.                 fwrite(user, sizeof(struct User), 1, fp2);// 将数据写入到文件中,释放内存
  451.                 user_temp = user;
  452.                 user = user->next;
  453.                 free(user_temp);
  454.         }

  455.         fclose(fp1);//关闭文件!!!!
  456.         fclose(fp2);

  457.         while (user_pool != NULL){//释放内存池
  458.                 user_temp = user_pool;
  459.                 user_pool = user_pool->next;
  460.                 free(user_temp);
  461.         }
  462.         while (book_pool != NULL){//释放内存池
  463.                 book_temp = book_pool;
  464.                 book_pool = book_pool->next;
  465.                 free(book_temp);
  466.         }
  467. }

  468. int main(void){
  469.         struct Book *book = NULL;
  470.         struct User *user = NULL;
  471.         char name[32];
  472.         int i;

  473.         welcome(&book, &user);

  474.         while (1){
  475.                 printf("\n请输入指令代码:");
  476.                 scanf("%d", &i);
  477.                 switch (i){
  478.                         case 1: increase(&user);break;
  479.                         case 2: if (log_on(user)){
  480.                                         consumer();
  481.                                         while (1){
  482.                                                 printf("\n请输入指令代码:");
  483.                                                 scanf("%d", &i);
  484.                                                 switch (i){
  485.                                                         case 1: searchEvent(book);break;
  486.                                                         case 2: print(book);break;
  487.                                                         case 3:if (administrator()){
  488.                                                                         manage();
  489.                                                                         while (1){
  490.                                                                                 printf("\n请输入指令代码:");
  491.                                                                                 scanf("%d", &i);
  492.                                                                                 switch (i){
  493.                                                                                         case 1: insert(&book);break;
  494.                                                                                         case 2: change(&book);break;
  495.                                                                                         case 3: delete(&book);break;
  496.                                                                                         case 4: print(book);break;
  497.                                                                                         case 5: modify(user);break;
  498.                                                                                         case 6: query(user);break;
  499.                                                                                         case 7: reduce(&user);break;
  500.                                                                                         case 8: display(user);break;
  501.                                                                                         case 9: enter(book, user);exit(i);
  502.                                                                                 }
  503.                                                                         }
  504.                                                                 }
  505.                                                                 else{
  506.                                                                         printf("密码错误\n");
  507.                                                                 }break;
  508.                                                         case 4: enter(book, user);exit(i);
  509.                                                 }
  510.                                         }
  511.                                 }
  512.                                 else{
  513.                                         printf("密码错误\n");
  514.                                 }break;
  515.                         case 3: enter(book, user);exit(i);
  516.                 }
  517.         }
  518.         return 0;
  519. }
复制代码
最佳答案
2022-12-9 22:18:14
void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
        FILE *fp1, *fp2;
        struct Book *temp_book = NULL // 请分配内存空间
        struct User *temp_user = NULL // 请分配内存空间

最佳答案

查看完整内容

void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明 FILE *fp1, *fp2; struct Book *temp_book = NULL // 请分配内存空间 struct User *temp_user = NULL // 请分配内存空间
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-9 22:18:14 | 显示全部楼层    本楼为最佳答案   
void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
        FILE *fp1, *fp2;
        struct Book *temp_book = NULL // 请分配内存空间
        struct User *temp_user = NULL // 请分配内存空间
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-9 22:27:39 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-9 23:10:07 | 显示全部楼层
ba21 发表于 2022-12-9 22:18
void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
        FILE *f ...

谢谢,明白了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 00:10:06 | 显示全部楼层
ba21 发表于 2022-12-9 22:27
参考下https://baike.baidu.com/item/fread/10942353?fr=aladdin

[`WTDYRB3UM4GV65~Z7VAQD.png 稍微改了一下
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define MAX 1024

  5. struct Book *book_pool = NULL;//内存池
  6. int count_book = 0;
  7. struct User *user_pool = NULL;
  8. int count_user = 0;

  9. struct Date{
  10.         int year;
  11.         int month;
  12.         int day;
  13. };

  14. struct Book{
  15.         char name[40];//书名
  16.         char author[128];//作者
  17.         char publisher[40];//出版社
  18.         int exist;
  19.         struct Date date;//日期
  20.         struct Book *next;
  21. };

  22. struct User{
  23.         char name[40];//用户名
  24.         char cypher[32];//密码
  25.         struct Date date;//注册日期
  26.         struct User *next;
  27. };

  28. void welcome(struct Book **book, struct User **user);//打印欢迎和使用说明
  29. void consumer(void);//打印用户使用说明
  30. void manage(void);//打印管理员使用说明
  31. int log_on(struct User *head);//登录
  32. int administrator(void);//登录管理员
  33. //用户相关的函数
  34. struct User *seek_user(struct User *head);//寻找用户,返回地址
  35. void print_user_one(struct User *head);//根据地址打印用户的信息
  36. void increase(struct User **head);//增加用户
  37. void reduce(struct User **head);//减少用户
  38. void query(struct User *book);//查询用户信息
  39. void modify(struct User *head);//修改用户信息
  40. void display(struct User *head);//显示所有用户信息
  41. //图书相关的函数
  42. struct Book *seek_book(struct Book *head);//寻找图书,返回地址
  43. void print_book_one(struct Book *head);//根据地址打印图书的信息
  44. void insert(struct Book **head);//录入新的图书
  45. void searchEvent(struct Book *book);//查找已有的图书
  46. void change(struct Book **head);//更改已有的图书
  47. void delete(struct Book **head);//删除已有的图书
  48. void print(struct Book *head);//打印所有的图书信息
  49. void enter(struct Book *book, struct User *user);//结束程序时将信息放入文件
  50. void borrow(void);//借书/
  51. void still(void);//还书/

  52. void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
  53.         FILE *fp1, *fp2;
  54.         struct Book *temp_book = NULL, *book;
  55.         struct User *temp_user = NULL, *user;

  56.         book = *head_book;
  57.         user = *head_user;

  58.         printf("| 欢迎使用图书馆程序 |\n");
  59.         printf("|------ 1:注册 ------|\n");
  60.         printf("|------ 2:登录 ------|\n");
  61.         printf("|------ 3:退出 ------|\n");
  62.         printf("|--------------------|\n");

  63.         if ((fp1 = fopen("book.txt", "rb")) == NULL){//fopen获取文件指针
  64.                 printf("打开文件失败!\n");
  65.                 exit(EXIT_FAILURE);
  66.         }
  67.         while (1){
  68.                 if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  69.                         temp_book = book_pool;
  70.                         book_pool = book_pool->next;
  71.                         count_book--;
  72.                 }
  73.                 else{//如果内存池空,使用malloc申请内存
  74.                         temp_book = (struct Book *)malloc(sizeof(struct Book));
  75.                         if(temp_book == NULL){
  76.                                 printf("内存分配失败了。\n");
  77.                                 exit(EXIT_FAILURE);
  78.                         }
  79.                 }

  80.                 fread(temp_book, sizeof(struct Book), 1, fp1);// 将文件数据读取出来
  81.                 if (feof(fp1)){
  82.                         if (temp_user == NULL){
  83.                                 free(temp_book);
  84.                         }
  85.                         break;
  86.                 }

  87.                 if (*head_book != NULL){
  88.                         temp_book = *head_book;
  89.                         *head_book = book;
  90.                         book->next = temp_book;
  91.                 }
  92.                 else{
  93.                         *head_book = book;
  94.                         book->next = NULL;
  95.                 }
  96.         }
  97.         fclose(fp1);//关闭文件!!!!

  98.         if ((fp2 = fopen("user.txt", "rb")) == NULL){//fopen获取文件指针
  99.                 printf("打开文件失败!\n");
  100.                 exit(EXIT_FAILURE);
  101.         }
  102.         while (1){
  103.                 if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  104.                         temp_user = user_pool;
  105.                         user_pool = user_pool->next;
  106.                         count_user--;
  107.                 }
  108.                 else{//如果内存池空,使用malloc申请内存
  109.                         temp_user = (struct User *)malloc(sizeof(struct User));
  110.                         if(temp_user == NULL){
  111.                                 printf("内存分配失败了。\n");
  112.                                 exit(EXIT_FAILURE);
  113.                         }
  114.                 }

  115.                 fread(temp_user, sizeof(struct User), 1, fp2);// 将文件数据读取出来
  116.                 if (feof(fp2)){
  117.                         if (temp_user == NULL){
  118.                                 free(temp_user);
  119.                         }
  120.                         break;
  121.                 }

  122.                 if (*head_user != NULL){
  123.                         temp_user = *head_user;
  124.                         *head_user = user;
  125.                         user->next = temp_user;
  126.                 }
  127.                 else{
  128.                         *head_user = user;
  129.                         user->next = NULL;
  130.                 }
  131.         }
  132.         fclose(fp2);
  133. }

  134. void consumer(void){//打印用户使用说明
  135.         printf("| 欢迎使用图书馆参观程序 |\n");
  136.         printf("|--- 1:查找已有图书 -----|\n");
  137.         printf("|--- 2:显示当前图书库 ---|\n");
  138.         printf("|--- 3:进入管理员模式 ---|\n");
  139.         printf("|--- 4:进入管理员模式 ---|\n");
  140.         printf("|--- 5:进入管理员模式 ---|\n");
  141.         printf("|--- 6:退出图书馆程序 ---|\n");
  142.         printf("|----------------------|\n");
  143. }

  144. void manage(void){//打印管理员使用说明
  145.         printf("| 欢迎使用图书馆管理程序 |\n");
  146.         printf("|--- 1:插入新的图书 -----|\n");
  147.         printf("|--- 2:更改已有图书 -----|\n");
  148.         printf("|--- 3:删除已有图书 -----|\n");
  149.         printf("|--- 4:显示当前图书库 ---|\n");
  150.         printf("|--- 5:更改用户信息 -----|\n");
  151.         printf("|--- 6:查询用户信息 -----|\n");
  152.         printf("|--- 7:删除用户信息 -----|\n");
  153.         printf("|--- 8:显示用户名单 -----|\n");
  154.         printf("|--- 9:退出图书馆程序 ---|\n");
  155.         printf("|----------------------|\n");
  156. }

  157. int log_on(struct User *head){//登录/
  158.         struct User *user;
  159.         char mm[32];
  160.         user = seek_user(head);
  161.         printf("请输入密码:");
  162.         scanf("%s", mm);
  163.         while (user != NULL){
  164.                 if (!strcmp(user->cypher, mm)){
  165.                         printf("登录成功");
  166.                         return 1;
  167.                 }
  168.                 user = user->next;
  169.         }
  170.         printf("登录失败");
  171.         return 0;
  172. }

  173. int administrator(void){
  174.         char mm[32];

  175.         printf("请输入管理员密码:");
  176.         scanf("%s", mm);
  177.         if (!strcmp("123456", mm)){
  178.                 printf("登录成功");
  179.                 return 1;
  180.         }
  181.         printf("登录失败");
  182.         return 0;
  183. }

  184. struct User *seek_user(struct User *head){//寻找用户,返回地址
  185.         struct User *target;
  186.         char name[40];

  187.         printf("请输入用户:");
  188.         scanf("%s", name);

  189.         target = head;
  190.         while (target != NULL && strcmp(target->name, name)){
  191.                 target = target->next;
  192.         }

  193.         return target;
  194. }

  195. void print_user_one(struct User *head){//根据地址打印用户的信息
  196.         printf("密码:%s\n", head->cypher);
  197.         printf("注册日期(aa-bb-cc):%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  198. }

  199. void increase(struct User **head){//增加用户/
  200.         struct User *user;
  201.         struct User *temp;

  202.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  203.                 user = user_pool;
  204.                 user_pool = user_pool->next;
  205.                 count_user--;
  206.         }
  207.         else{//如果内存池空,使用malloc申请内存
  208.                 user = (struct User *)malloc(sizeof(struct User));
  209.                 if(user == NULL){
  210.                         printf("内存分配失败了。\n");
  211.                         exit(1);
  212.                 }
  213.         }
  214.         printf("请输入用户名:");
  215.         scanf("%s", user->name);
  216.         printf("请设置密码:");
  217.         scanf("%s", user->cypher);
  218.         printf("请输入注册日期(aa-bb-cc)");
  219.         scanf("%d-%d-%d", &user->date.year, &user->date.month, &user->date.day);

  220.         if (*head != NULL){
  221.                 temp = *head;
  222.                 *head = user;
  223.                 user->next = temp;
  224.         }
  225.         else{
  226.                 *head = user;
  227.                 user->next = NULL;
  228.         }
  229. }

  230. void reduce(struct User **head){//减少用户
  231.         struct User *target;//目标
  232.         struct User *now;
  233.         struct User *temp;//上一次

  234.         target = seek_user(*head);// 先找到待删除的节点指针

  235.         if (target == NULL){
  236.                 printf("找不到该用户!\n");
  237.         }
  238.         else{
  239.                 now = *head;
  240.                 temp = NULL;

  241.                 while (now != NULL && now != target){// current定位到待删除的节>点
  242.                         temp = now;
  243.                         now = now->next;
  244.                 }
  245.                 if (temp == NULL){// 待删除的节点是第一个节点
  246.                         *head = now->next;
  247.                 }
  248.                 else{// 待删除的节点不是第一个节点
  249.                         temp->next = now->next;
  250.                 }

  251.                 if (count_user < MAX){//判断内存池有没有空位
  252.                         if (user_pool != NULL){
  253.                                 temp = user_pool;
  254.                                 user_pool = target;
  255.                                 target->next = NULL;
  256.                         }                        
  257.                         else{
  258.                                 user_pool = target;
  259.                                 target->next = NULL;
  260.                         }
  261.                         count_user++;
  262.                 }
  263.                 else{
  264.                         free(target);
  265.                 }
  266.         }
  267. }


  268. void query(struct User *head){//查询用户信息
  269.         struct User *user;
  270.         char name[32];

  271.         printf("请输入用户名:");
  272.         scanf("%s", name);

  273.         user = head;

  274.         while (user != NULL){
  275.                 if (!strcmp(user->name, name)){
  276.                         break;
  277.                 }
  278.                 user = user->next;
  279.         }
  280.         if(user == NULL){
  281.                 printf("抱歉,没有找到。\n");
  282.         }
  283.         else{
  284.                 print_user_one(user);
  285.         }
  286. }

  287. void modify(struct User *head){//修改用户信息/
  288.         struct User *target;

  289.         char name[32];

  290.         target = seek_user(head);// 先找到待更改的节点指针

  291.         if (target == NULL){
  292.                 printf("找不到该书名!\n");
  293.         }
  294.         else{
  295.                 printf("请设置用户名:");
  296.                 scanf("%s", target->name);
  297.               printf("请设置密码:");
  298.                 scanf("%s", target->cypher);
  299.                 printf("请设置注册日期(aa-bb-cc)");
  300.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  301.         }
  302. }

  303. void display(struct User *head){//显示所有用户信息/
  304.         struct User *now;

  305.         now = head;
  306.         while (now != NULL){
  307.                 printf("用户名:%s\n", now->name);
  308.                 print_user_one(now);
  309.                 now = now->next;
  310.         }
  311. }
  312. //--------------------------图书------------------------------
  313. struct Book *seek_book(struct Book *head){//寻找图书,返回地址
  314.         struct Book *target;
  315.         char name[40];
  316.    
  317.         printf("请输入书名:");
  318.         scanf("%s", name);
  319.    
  320.         target = head;
  321.         while (target != NULL && strcmp(target->name, name)){
  322.                 target = target->next;
  323.         }
  324.    
  325.         return target;
  326. }

  327. void print_one(struct Book *head){//根据地址打印图书的信息
  328.         printf("作者:%s\n", head->author);
  329.         printf("出版社:%s\n", head->publisher);
  330.         printf("日期:%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  331.         printf("书籍状态:");
  332.         switch (head->exist){
  333.                 case 1: printf("可借取\n");break;
  334.                 case 0: printf("不可借取\n");break;
  335.         }
  336. }

  337. void insert(struct Book **head){//录入新的图书
  338.         struct Book *book;
  339.         struct Book *temp;

  340.         if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  341.                 book = book_pool;
  342.                 book_pool = book_pool->next;
  343.                 count_book--;
  344.         }
  345.         else{//如果内存池空,使用malloc申请内存
  346.                 book = (struct Book *)malloc(sizeof(struct Book));
  347.                 if(book == NULL){
  348.                         printf("内存分配失败了。\n");
  349.                         exit(1);
  350.                 }
  351.         }
  352.         printf("请输入书名:");
  353.         scanf("%s", book->name);
  354.         printf("请输入作者:");
  355.         scanf("%s", book->author);
  356.         printf("请输入出版社:");
  357.         scanf("%s", book->publisher);
  358.         printf("请输入日期(aa-bb-cc):");
  359.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  360.         book->exist = 1;

  361.         if (*head != NULL){
  362.                 temp = *head;
  363.                 *head = book;
  364.                 book->next = temp;
  365.         }
  366.         else{
  367.                 *head = book;
  368.                 book->next = NULL;
  369.         }
  370.         putchar('\n');
  371. }


  372. void searchEvent(struct Book *head){//查找已有的书名
  373.         struct Book *book;
  374.         char name[32];

  375.         printf("请输入书名:");
  376.         scanf("%s", name);

  377.         book = head;
  378.        
  379.         while (book != NULL){
  380.                 if (!strcmp(book->name, name)){
  381.                         break;
  382.                 }
  383.                 book = book->next;
  384.         }
  385.         if(book == NULL){
  386.                 printf("抱歉,没有找到。\n");
  387.         }
  388.         else{
  389.                 print_one(book);
  390.         }
  391. }

  392. void change(struct Book **head){//更改
  393.         struct Book *target;

  394.         char name[32];

  395.         target = seek_book(*head);// 先找到待更改的节点指针

  396.         if (target == NULL){
  397.                 printf("找不到该书名!\n");
  398.         }
  399.         else{
  400.                 printf("请输入作者:");
  401.                 scanf("%s", target->author);
  402.                 printf("请输入出版社:");
  403.                 scanf("%s", target->publisher);
  404.                 printf("请输入日期(aa-bb-cc):");
  405.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  406.                 printf("是否可借取(1/0):");
  407.                 scanf("%d", &target->exist);
  408.         }
  409. }

  410. void delete(struct Book **head){//删除
  411.         struct Book *target;//目标
  412.         struct Book *now;
  413.         struct Book *temp;//上一次

  414.         target = seek_book(*head);// 先找到待删除的节点指针

  415.         if (target == NULL){
  416.                 printf("找不到该书名!\n");
  417.         }
  418.         else{
  419.                 now = *head;
  420.                 temp = NULL;
  421.    
  422.                 while (now != NULL && now != target){// current定位到待删除的节点
  423.                         temp = now;
  424.                         now = now->next;
  425.                 }
  426.                 if (temp == NULL){// 待删除的节点是第一个节点
  427.                         *head = now->next;
  428.                 }

  429.                 else{// 待删除的节点不是第一个节点
  430.                         temp->next = now->next;
  431.                 }

  432.                 if (count_book < MAX){//判断内存池有没有空位
  433.                         if (book_pool != NULL){
  434.                                 temp = book_pool;
  435.                                 book_pool = target;
  436.                                 target->next = NULL;
  437.                         }
  438.                         else{
  439.                                 book_pool = target;
  440.                                 target->next = NULL;
  441.                         }
  442.                         count_book++;
  443.                 }
  444.                 else{
  445.                         free(target);
  446.                 }
  447.         }
  448. }

  449. void print(struct Book *head){//打印
  450.         struct Book *now;

  451.         now = head;
  452.         while (now != NULL){
  453.                 printf("书名:%s\n", now->author);
  454.                 print_one(now);
  455.                 now = now->next;
  456.         }
  457. }

  458. void enter(struct Book *book, struct User *user){//结束程序时将图书信息放入文件
  459.         struct Book *book_temp;
  460.         struct User *user_temp;
  461.         FILE *fp1, *fp2;

  462.         if ((fp1 = fopen("book.txt", "wb")) == NULL){//fopen获取文件指针
  463.                 printf("打开文件失败!\n");
  464.                 exit(EXIT_FAILURE);
  465.         }

  466.         if ((fp2 = fopen("user.txt", "wb")) == NULL){//fopen获取文件指针
  467.                 printf("打开文件失败!\n");
  468.                 exit(EXIT_FAILURE);
  469.         }

  470.         while (book != NULL){
  471.                 fwrite(book, sizeof(struct Book), 1, fp1);// 将数据写入到文件中,释放内存
  472.                 book_temp = book;
  473.                 book = book->next;
  474.                 free(book_temp);
  475.         }

  476.         while (user != NULL){
  477.                 fwrite(user, sizeof(struct User), 1, fp2);// 将数据写入到文件中,释放内存
  478.                 user_temp = user;
  479.                 user = user->next;
  480.                 free(user_temp);
  481.         }

  482.         fclose(fp1);//关闭文件!!!!
  483.         fclose(fp2);

  484.         while (user_pool != NULL){//释放内存池
  485.                 user_temp = user_pool;
  486.                 user_pool = user_pool->next;
  487.                 free(user_temp);
  488.         }
  489.         while (book_pool != NULL){//释放内存池
  490.                 book_temp = book_pool;
  491.                 book_pool = book_pool->next;
  492.                 free(book_temp);
  493.         }
  494. }

  495. int main(void){
  496.         struct Book *book = NULL;
  497.         struct User *user = NULL;
  498.         char name[32];
  499.         int i;

  500.         welcome(&book, &user);

  501.         while (1){
  502.                 printf("\n请输入指令代码:");
  503.                 scanf("%d", &i);
  504.                 switch (i){
  505.                         case 1: increase(&user);break;
  506.                         case 2: if (log_on(user)){
  507.                                         consumer();
  508.                                         while (1){
  509.                                                 printf("\n请输入指令代码:");
  510.                                                 scanf("%d", &i);
  511.                                                 switch (i){
  512.                                                         case 1: searchEvent(book);break;
  513.                                                         case 2: ;break;
  514.                                                         case 3: ;break;
  515.                                                         case 4: print(book);break;
  516.                                                         case 5:if (administrator()){
  517.                                                                         manage();
  518.                                                                         while (1){
  519.                                                                                 printf("\n请输入指令代码:");
  520.                                                                                 scanf("%d", &i);
  521.                                                                                 switch (i){
  522.                                                                                         case 1: insert(&book);break;
  523.                                                                                         case 2: change(&book);break;
  524.                                                                                         case 3: delete(&book);break;
  525.                                                                                         case 4: print(book);break;
  526.                                                                                         case 5: modify(user);break;
  527.                                                                                         case 6: query(user);break;
  528.                                                                                         case 7: reduce(&user);break;
  529.                                                                                         case 8: display(user);break;
  530.                                                                                         case 9: enter(book, user);exit(i);
  531.                                                                                 }
  532.                                                                         }
  533.                                                                 }
  534.                                                                 else{
  535.                                                                         printf("密码错误\n");
  536.                                                                 }
  537.                                                                 break;
  538.                                                         case 6: enter(book, user);exit(i);
  539.                                                 }
  540.                                         }
  541.                                 }
  542.                                 else{
  543.                                         printf("密码错误\n");
  544.                                 }
  545.                                 break;
  546.                         case 3: enter(book, user);exit(i);
  547.                 }
  548.         }
  549.         return 0;
  550. }
复制代码
,然后不知道哪里错了,感觉是这里有问题,但是又没有改这个部分
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 22:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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