免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1803 | 回复: 1
打印 上一主题 下一主题

[C] 求助-关于用C语言编写存储、查询图书馆书籍信息程序出错的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-29 18:42 |只看该作者 |倒序浏览
最近,老师要我们用C语言编写一个存储、查询图书馆书记信息的程序。保存没有问题,但是即使输入一个正确的书目也查询不到。如果将书籍信息全都读取出来,得到的都是乱码。哪位高手指点、纠正一下小弟,不胜感激。先行谢过。程序如下:

#include<stdio.h>
#include<stdlib.h>

struct booklist_info
{
    char name[10];         //书籍名称
    char number[9];       //书籍书目
    char publisher[10];  //书籍出版社
};
typedef struct booklist_info booklist;

void writetofile(booklist *book1);
void readfromfile(booklist *book1);
void getlength(booklist *book1);

void main()
{
    booklist *book1,*book2;
    char ch;
    FILE *fp1;
    int length,i,flag = 0;   

    printf("what do you want to do?\n";
    printf("1.please input 'w' if you want to save the information of the book\n";
    printf("2.please input 'r' if you want to read the information of the book\n";
    scanf("%c",&ch);
    fflush(stdin);

    switch(ch)
    {
        case 'w':                     //保存书籍信息
        {
            book1 = (booklist *) malloc (sizeof(booklist));
            if(book1 == NULL)
            {
                printf("not enouh memory\n";
                getch();
                exit(0);
             }

            do
            {
                printf("please input the name of the book(0~9)\n";
                gets(book1->name);
                fflush(stdin);
                printf("please input the number of the book(0~\n";
                gets(book1->number);
                fflush(stdin);
                printf("please intput the publisher of the book(0~9)\n";
                gets(book1->publisher);
                fflush(stdin);
                writetofile(book1);

                printf("Do you want to continue to save the booklist?Yes(y) No(n)\n";
                scanf("%c",&ch);
                fflush(stdin);
             }while(ch == 'y');

            break;
         }
         case 'r':                //读取书籍信息
         {
            do
            {
                printf("please input the number of the book which you what to seek.(0~\n";
                book2 = (booklist *) malloc (sizeof(booklist));
                if(book2 == NULL)
                {
                    printf("not enouh memory\n";
                    getch();
                    exit(0);
                }
                    gets(book2->number);


                if((fp1 = fopen("d:\\book.dat","rb")) == NULL)
                {
                    printf("can not open the file\n");
                    getch();
                    exit(0);
                }


                for(i =0;i <= 2;i++)             //此处循环上限为2,因为只保存了3个书籍信息。
                {
                    fseek(fp1,i*sizeof(struct booklist_info),SEEK_SET);
                    fread(book1,sizeof(booklist),1,fp1);
                    if(book1->number == book2->number)
                    {
                        flag = 1;
                        printf("the name of the book:\n");
                        printf("%s",book1->name);
                        printf("\nthe number of the book:\n");
                        printf("%s",book1->number);
                        printf("\nthe publisher of the book:\n");
                        printf("%s",book1->publisher);
                        getch();
                    }
                }

                if(flag == 0)
                {
                    printf("can not find the book\n");
                    getch();
                }
                printf("Do you want to continue to seek the booklist?Yes(y) No(n)\n");
                scanf("%c",&ch);
                fflush(stdin);
            }while(ch == 'y');

             fclose(fp1);

           break;
          }
    }

    free(book1);
    free(book2);
}
void writetofile(booklist *book1)
{
    FILE *fp;

    if((fp = fopen("d:\\book.dat","a")) == NULL)
    {
        printf("can not build the file\n");
        getch();
        exit(0);
    }

        fwrite(book1,sizeof(booklist),1,fp);
        fclose(fp);
}

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2007-04-29 19:14 |只看该作者
花点时间把你的问题细化、具体化一下吧。例如说你在某处碰到问题,写个简单的例子(不要包含无关代码,突出问题),然后详细准确的描述你的疑惑。不要一上来就三不管的把你所有的代码都贴上来,别人哪来那么多闲情逸致帮你调程序。

[ 本帖最后由 MMMIX 于 2007-4-29 19:21 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP