humanint 2017-03-14 13: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 13:09
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?