gpt4 book ai didi

c - 如何显示所有价格低于 70.00 的书籍。从读取文件?

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:51 27 4
gpt4 key购买 nike

<分区>

struct book{
int code;
char title[20];
char author[20];
char publisher[20];
char year[4];
double price;
};

struct book book1, mybook[100];

FILE *tf, *bf;

int main(){
int i;

tf = fopen("mybook.txt","r");
bf = fopen("myEbooks","wb");

for(i=1; i<100; i++){
fscanf(tf,"%i\n", &book1.code);
fscanf(tf,"%[^\n]\n", book1.title);
fscanf(tf,"%[^\n]\n", book1.author);
fscanf(tf,"%[^\n]\n", book1.publisher);
fscanf(tf,"%[^\n]\n", &book1.year);
fscanf(tf,"%lf\n", &book1.price);

fwrite(&book1, sizeof(struct book),1, bf);

}
printf("\nBinary Stock file successfully generated!!\n");

fclose(tf);
fclose(bf);

return 0;
}

上面的代码用于从读取 .txt 文件生成二进制文件。下面的代码用于读取二进制文件。

struct book{
int code;
char title[20];
char author[20];
char publisher[20];
char year[4];
double price;
};

int main(){
int data;
int record;
double price;
struct book book1;

FILE * bf;

bf = fopen("myEbooks", "rb");

fseek(bf,1,SEEK_END);
record = (ftell(bf)/sizeof(struct book));
rewind(bf);

printf("Title\t\t Code\t\tAuthor\t\tPublisher\t Year\tPrice\n");

for(data=0;data<100;data++){
fread(&book1, sizeof(struct book), 1, bf);

printf("%s\t\t %i\t %s\t %s\t %s\t %0.2lf\n", book1.title, book1.code, book1.author, book1.publisher, book1.year, book1.price);
}

fclose(bf);
return 0;
}
  • 如何插入条件?
  • 我需要在哪里插入条件语句?
  • 如何只显示价格低于 70.00 的图书。从读取文件?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com