标题:求大神帮忙~
只看楼主
qiankai
Rank: 2
等 级:论坛游民
帖 子:35
专家分:49
注 册:2012-4-22
结帖率:100%
 问题点数:0 回复次数:1 
求大神帮忙~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max 30
 typedef struct {
         long int Nub;
         char BookName[Max];
         char AuthorName[Max];
         long int FLH;
         char PubUnit[Max];
         char PubTime[Max];
         char price[Max];
         }Node;
 int Input( char FileName[])
 {
     Node G;
     int i = 0;
     FILE * fp ;
     fp = fopen(FileName, "a+");
     printf("请输入登录号\n");
     scanf("%ld", &G.Nub);
     getchar();
     printf("请输入书名\n");
     gets(G.BookName);
     printf("请输入作者名\n");
     gets(G.AuthorName);
     printf("请输入分类号\n");
     scanf("%ld", &G.FLH);
     printf("请输入出版单位\n");
     getchar();
     gets(G.PubUnit);
     printf("请输入出版时间\n");
     gets(G.PubTime);
     printf("请输入价格\n");
     gets(G.price);
     printf("%s\n", G.price);
     fwrite(&G, sizeof(G), 1, fp);
     fclose(fp);
     return 0;
  }
 int Print(char FileName[])
 {
      Node book;
      FILE * fp ;
      fp = fopen(FileName, "a+");
      while(1){
               book.AuthorName[0] = '@';
               fread(&book, sizeof(book), 1, fp);
               if(book.AuthorName[0] == '@'){
                                     break;
                                     }   
               printf("      登录号:%ld\n", book.Nub);
               printf("      书名:%s\n", book.BookName);
               printf("      作者名:%s\n", book.AuthorName);
               printf("      分类号:%ld\n", book.FLH);
               printf("      出版单位:%s\n", book.PubUnit);
               printf("      出版时间:%s\n", book.PubTime);
               printf("      价格:%s\n", book.price);
               printf("-------data-------\n");  
               }  
               fclose(fp);  
               return 0;  
     }
 void Delet();
 int Revise(char FileName[])
 {
     int i = 0, j;
     Node book, newbook;
     printf("请输入想要修改的图书的登录号\n");
     scanf("%ld", &newbook.Nub);
     FILE * fp ;
     fp = fopen(FileName, "r+");
      while(1){
               book.AuthorName[0] = '@';
               fread(&book, sizeof(book), 1, fp);
               if(book.AuthorName[0] == '@'){
                                     printf("Have not found! please try again !\n");
                                     return 0;
                                     }  
               i++;
               if(newbook.Nub == book.Nub){
                              break;
                              }
               }
               printf("%d\n", i);
      FILE * fq ;
      fq = fopen(FileName, "r+");
      if(i == 1){     
                     printf("请输入新的登录号\n");
                     scanf("%ld", &newbook.Nub);
                     printf("%ld\n", newbook.Nub);
                     getchar();
                     printf("请输入新的书名\n");
                     gets(newbook.BookName);
                     printf("请输入新的作者名\n");
                     gets(newbook.AuthorName);
                     printf("请输入新的分类号\n");
                     scanf("%ld", &newbook.FLH);
                     printf("请输入新的出版单位\n");
                     getchar();
                     gets(newbook.PubUnit);
                     printf("请输入新的出版时间\n");
                     gets(newbook.PubTime);
                     printf("请输入新的价格\n");
                     gets(newbook.price);
                     fwrite(&newbook, sizeof(newbook), 1, fq);
                     fclose(fq);
                     printf("修改成功!!!\n");
                     }
      if(i != 1){
           fseek(fq, (i-1)*sizeof(newbook)+i-1, 0);        
                     printf("请输入新的登录号\n");
                     scanf("%ld", &newbook.Nub);
                     printf("%ld\n", newbook.Nub);
                     getchar();
                     printf("请输入新的书名\n");
                     gets(newbook.BookName);
                     printf("请输入新的作者名\n");
                     gets(newbook.AuthorName);
                     printf("请输入新的分类号\n");
                     scanf("%ld", &newbook.FLH);
                     printf("请输入新的出版单位\n");
                     getchar();
                     gets(newbook.PubUnit);
                     printf("请输入新的出版时间\n");
                     gets(newbook.PubTime);
                     printf("请输入新的价格\n");
                     gets(newbook.price);
                     fwrite(&newbook, sizeof(newbook), 1, fq);
                     fclose(fq);
                     printf("修改成功!!!\n");
                 }
                 // }
                  }
                    
 int Search(int a, char FileName[])
 {
     if(a == 1){
          char FileName2[Max];
          printf("请输入要查找的书名\n");
          getchar();
          gets(FileName2);
          Node book;
          FILE * fp ;
          fp = fopen(FileName, "r");
          while(1){
               book.AuthorName[0] = '@';
               fread(&book, sizeof(book), 1, fp);
               if(book.AuthorName[0] == '@'){
                                     printf("None\n");
                                     break;
                                     }   
               if(strcmp(FileName2, book.BookName) == 0){
               printf(" Have found Sucessfully !!!\n");
               printf("      登录号:%ld\n", book.Nub);
               printf("      书名:%s\n", book.BookName);
               printf("      作者名:%s\n", book.AuthorName);
               printf("      分类号:%ld\n", book.FLH);
               printf("      出版单位:%s\n", book.PubUnit);
               printf("      出版时间:%s\n", book.PubTime);
               printf("      价格:%s\n", book.price);
               printf("-------data-------\n");  
               break;
               }
                               }
                               }
     if(a == 2){
          char AuthorName2[Max];
          printf("请输入要查找的作者名\n");
          getchar();
          gets(AuthorName2);
          Node book;
          FILE * fp ;
          fp = fopen(FileName, "r");
          while(1){
               book.BookName[0] = '@';
               fread(&book, sizeof(book), 1, fp);
               if(book.BookName[0] == '@'){
                                     printf("None\n");
                                     break;
                                     }   
               if(strcmp(AuthorName2, book.AuthorName) == 0){
               printf(" Have found Sucessfully !!!\n");
               printf("      登录号:%ld\n", book.Nub);
               printf("      书名:%s\n", book.BookName);
               printf("      作者名:%s\n", book.AuthorName);
               printf("      分类号:%ld\n", book.FLH);
               printf("      出版单位:%s\n", book.PubUnit);
               printf("      出版时间:%s\n", book.PubTime);
               printf("      价格:%s\n", book.price);
               printf("-------data-------\n");  
               break;
               }
                               }
                              
          fclose(fp);
          return 0;
          }
          }
           


 int main()
 {
     int i, m = 1;
     Node G;
     while(m = 1){
             printf("------图书信息管理系统------ \n1. 录入  2.输出  3.删除  4.修改  5.查询  0.退出\n请输入命令符\n------------end-------------\n" );
             scanf("%d", &i);
             switch  (i)
             {
              case 1:
                     char FileName[Max];
                     printf("请输入要录入到的文件名\n");
                     getchar();
                     gets(FileName);
                     Input(FileName);
                     break;
              case 2:
                     Print( FileName);
                     break;
              case 3:
                 // Delet();
                     break;
              case 4:
                     Revise(FileName);
                     break;
              case 5:
                     int a;
                     printf("按书名查找请输入'1',按作者名查找请输入'2' \n");
                     scanf("%d", &a);
                     Search(a, FileName);
                     break;
              case 0:
                     m = 0;
                    exit(1);
              default:
                     break;
                  }
                  }
     getchar();
     return 0;
     }
       在实现修改这个功能时,会出现这样个情况:当我把第二块数据块改掉,后面的数据块在实现输出功能的时候就会出现错位,求大神帮忙啊,我已经纠结了好几天了~~  另外我不太了解fopen"r+"这种打开方式,能给我讲讲fwrite是如何通过它怎样存储数据的吗?
搜索更多相关主题的帖子: long include price 
2012-12-15 22:19
qiankai
Rank: 2
等 级:论坛游民
帖 子:35
专家分:49
注 册:2012-4-22
得分:0 
我代码可能发的太长了,其实有问题的就只有那个做修改的函数块,除了修改这个功能有问题,其他功能都能实现了,不要都被这么长的代码吓跑了~
2012-12-15 22:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-391432-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.145503 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved