humanint 2017-03-14 05:06 采纳率: 100%
浏览 1528

c,请帮我看看这个程序,怎么都找不到错误在哪,编译没问题调试一直出错

图片说明
#include
#include
#include
#include
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
#define CONTINUE 0
#define DONE 1
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
struct pack
{
struct book book;
bool delete_me;
};
char * s_gets(char *st, int n);
int getlet(const char *s);
int getbook(struct pack*pb);
void update(struct pack*item);
int main(void)
{
struct pack library[MAXBKS];
int count = 0;
int deleted = 0;
int index, filecount, open;
FILE *pbooks;
int size = sizeof(struct book);

if ((pbooks = fopen("book.dat", "r")) != NULL)    //无法读取内存?但这里我看了很多遍都没有问题呀
    while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
    {
        if (count == 0)
            puts("current contents of book.dat:");
        printf("%s by %s:$%.2f\n", library[count].book.title,
            library[count].book.author, library[count].book.value);
        printf("do you wish to change or delete this entry?<y/n> ");
        if (getlet("yn") == 'y')
        {
            printf("enter c to change,d to delete entry:");
            if (getlet("cd") == 'd')
            {
                library[count].delete_me = true;
                deleted++;
                puts("entry marked for deletion.");
            }
            else
                update(&library[count]);
        }
        count++;
    }
fclose(pbooks);
filecount = count - deleted;
if (count == MAXBKS)
{
    fputs("the book.dat is full.", stderr);
    exit(EXIT_FAILURE);
}
puts("add new book titles.");
puts("press [enter] at the start of a line to stop.");
open = 0;
while (filecount < MAXBKS)
{
    if (filecount < count)
    {
        while (library[open].delete_me == false)
            open++;
        if (getbook(&library[open]) == DONE)
            break;
    }
    else if (getbook(&library[open]) == DONE)
        break;
    filecount++;
    if (filecount < MAXBKS)
        puts("enter the next book title:");
}
puts("here is the list of your book:");
for (index = 0; index < filecount; index++)
    if (library[index].delete_me == false)
        printf("%s by %s:$%.2f\n", library[index].book.title,
            library[index].book.author, library[index].book.value);
if ((pbooks = fopen("book.dat", "w")) == NULL)
{
    fputs("can't open book.dat for output.", stderr);
    exit(EXIT_FAILURE);
}
for (index = 0; index < filecount; index++)
    if (library[index].delete_me == false)
        fwrite(&(library[index].book), size, 1, pbooks);
fclose(pbooks);
puts("done");
getchar();
return 0;

}

int getlet(const char * s)
{
char c;
c = getchar();
while (strchr(s,c)==NULL)
{
printf("enter a character in the list %s\n", s);
while (getchar() != '\n');
c = getchar();
}
while (getchar() != '\n');
return c;
}

int getbook(struct pack * pb)
{
int status = CONTINUE;
if (s_gets(pb->book.title,MAXTITL) == NULL || pb->book.title[0] == '\0')
status = DONE;
else
{
puts("enter the author:");
s_gets(pb->book.author, MAXAUTL);
puts("enter the value:");
while (scanf("%f", &pb->book.value) != 1)
{
puts("use numeric input:");
scanf("%*s");
}
while (getchar() != '\n');
pb->delete_me = false;
}
return status;
}

void update(struct pack * item)
{
struct book copy;
char c;
copy = item->book;
puts("enter the letter that indicates your choice:");
puts("t)modify title a)modify author");
puts("v)modify value s)saving changes");
puts("q)quit,ignore changes");
while ((c = getlet("tavsq")) != 's'&&c != 'q')
{
switch (c)
{
case't':puts("enter new title:");
s_gets(copy.title, MAXTITL);
break;
case'a':puts("enter new author:");
s_gets(copy.author, MAXAUTL);
break;
case'v':puts("enter new value:");
while (scanf("%f", &copy.value) != 1)
{
puts("enter a numeric value:");
scanf("%*s");
}
while (getchar() != '\n');
break;
}
puts("enter the letter that indicates your choice:");
puts("t)modify title a)modify author");
puts("v)modify value s)saving changes");
puts("q)quit,ignore changes");
}
if (c == 's');
item->book = copy;
}

char * s_gets(char * st, int n)
{
char *ret_val;
char *find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n');
}
return ret_val;
}

展开全部

  • 写回答

6条回答 默认 最新

  • 锁柱子 2017-03-14 05:09
    关注

    book.bat的路径写对了吗?这种写法应该是放在项目根目录下吧

    评论
  • 坤昱 《一起学习C语言》专栏作者 2017-03-14 06:32
    关注

    pbooks指向的是空地址,无法获取内存值。

    评论
  • cduttm 2017-03-14 07:28
    关注

    pbooks = fopen("book.dat", "r")) != NULL这句话应该是先赋值再判空吧?空的时候就异常了,不懂说得对不对,你试试看!电脑上没环境我就不尝试了。

    评论
  • 锁柱子 2017-03-15 19:13
    关注

    图片说明
    r方式文件必须存在,你可以换成a+.我怀疑你看了假书

    评论
  • happyboycwh 2017-03-15 19:36
    关注

    1、初始化指针,否则为野指针
    FILE *pbooks=NULL;
    2、if语句记得加{}后再写中间的代码,否则if和while作为一句执行
    if ((pbooks = fopen("book.dat", "r")) != NULL) //无法读取内存?但这里我看了很多遍都没有问题呀
    {
    while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
    .............

    }

    评论
  • Infinite_Ryvius 2017-06-18 03:15
    关注

    应该是你第一次的 fclose(pbooks);的问题
    因为如果第一次运行程序的时候,是不存在book.dat的。
    所以pbook是没有指向文件的,然后你又释放文件——fclose(pbooks);
    而pbook本身是没有指向文件的,所有释放不了导致出错。
    应该把fclose(pbooks);放到 if 的中:
    if ((pbooks = fopen("book.dat", "r")) != NULL)
    {
    //无法读取内存?但这里我看了很多遍都没有问题呀
    while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
    {
    if (count == 0)
    puts("current contents of book.dat:");
    printf("%s by %s:$%.2f\n", library[count].book.title,
    library[count].book.author, library[count].book.value);
    printf("do you wish to change or delete this entry? ");
    if (getlet("yn") == 'y')
    {
    printf("enter c to change,d to delete entry:");
    if (getlet("cd") == 'd')
    {
    library[count].delete_me = true;
    deleted++;
    puts("entry marked for deletion.");
    }
    else
    update(&library[count]);
    }
    count++;
    }
    fclose(pbooks);
    }

    展开全部

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 kubeasz部署遇到问题
  • ¥15 GUIDE to App Designer Migration Tool for MATLAB
  • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
  • ¥15 hbuilderx写一个这个网页🙏
  • ¥20 plant simulation与python com接口实时数据交互
  • ¥15 有关汽车的MC9S12XS128单片机实验
  • ¥15 求c语言动态链表相关课程有偿,或能将这块知识点讲明白
  • ¥15 FLKT界面刷新异常
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥50 单细胞测序拟时序分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部