69,371
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
struct book
{
char title[MAX];
char author[MAX];
float value;
};
void EatLine (void);
int main (void)
{
FILE *pf;
int inum;
int icount = 0;
size_t isize;
size_t isumsize;
isize = sizeof(struct book);
struct book library;
if ((pf = fopen("xb.txt", "r+b")) == NULL)
{
puts("Can't open xb.txt.....");
exit(EXIT_FAILURE);
}
while (fread(&library, isize, 1, pf) == 1)
{
printf("%d, %s by %s $%.2f \n",icount, library.title, library.author, library.value);
icount++;
}
printf("Which number you want to select...\n");
while (scanf("%d", &inum) == 1)
{
EatLine();
isumsize = inum * sizeof(struct book);
fseek(pf, isumsize, SEEK_SET);
fread(&library, isize, 1, pf);
printf("%s by %s $%.2f \n",library.title, library.author, library.value);
puts("Enter the new title;");
gets(library.title);
puts("Enter the new author");
gets(library.author);
puts("Enter the new valule");
scanf("%f", &library.value);
fseek(pf, isumsize, SEEK_SET);
fwrite(&library, isize, 1, pf);
fseek(pf, isumsize, SEEK_SET);
fread(&library, isize, 1, pf);
printf("%s by %s $%.2f \n",library.title, library.author, library.value);
EatLine();
}
fclose(pf);
return 0;
}
void EatLine (void)
{
while (getchar() != '\n')
{
continue;
}
}