#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", ©.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;
}