69,382
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 10
#define MAXBKS 20
void Delete (struct book *);
void Alter (struct book *);
void EatLine (void);
struct book
{
char title[MAXBKS];
char author[MAXBKS];
float value;
};
int main (void)
{
struct book library[10];
FILE *pf;
int index;
int count = 0; // read calc
int choose; // n11
size_t size = sizeof(struct book);
if ((pf =fopen("xb.txt", "a+b")) == NULL)
{
puts("Can't open xb.txt");
exit(EXIT_FAILURE);
}
// rewind(pf);
while (count < LEN && fread(&library[count], size, 1, pf) == 1)
{
printf("%d. %s by %s $%.2f....\n", count+1, library[count].title, library[count].author, library[count].value);
count++;
}
puts("select a item to alter......");
puts("1. delete 2.alter");
while (scanf("%d", &choose) == 1)
{
switch (choose)
{
case 1: Delete(library); break;
case 2: Alter(library); break;
default : puts("not 1 or 2 ....");
}
puts("Enter others choose....");
}
rewind(pf);
for (index = 0; index < count; index++)
{
fwrite(&library, size, 1, pf);
}
return 0;
}
void Alter (struct book *pC)
{
int i;
puts("Enter the number you want to alter");
scanf("%d", &i);
EatLine();
puts("Enter the new title");
gets(pC[i-1].title);
puts("Enter the new author");
gets(pC[i-1].author);
puts("Enter the value...");
scanf("%f", &pC[i-1].value);
EatLine();
}
void Delete (struct book *pC)
{
int i;
puts("Enter the number your want to delete..");
scanf("%d", &i);
strcpy(pC[i-1].title, " ");
strcpy(pC[i-1].author, " ");
EatLine();
}
void EatLine (void)
{
while (getchar() != '\n')
{
continue;
}
}
pf =fopen("xb.txt", "a+b")