CTest[1]Struct综合

简介:
#include <stdio.h>
#include <stdlib.h>
#define MAXTITL 40
#define MAXAUTH 40
#define MAXBKS 10
struct book {
    char title[MAXTITL];
    char author[MAXAUTH];
    float value;
};
int main(void){
    struct book library[MAXBKS];
    int count=0;
    int index,filecount;
    FILE *pbooks;
    int size=sizeof(struct book);
    if((pbooks=fopen("book.dat","a+b"))==NULL){
        fputs("Can't open book.dat file.\n",stderr);
        exit(1);
    }
    rewind(pbooks);
    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].title,library[count].author,library[count].value);
        count++;
    }
    filecount=count;
    if(count==MAXBKS){
        fputs("The book.dat file is full.",stderr);
        exit(2);
    }
    puts("Please add new book titles.");
    puts("Press [enter] at the start of a line to stop.");
    while(count<MAXBKS&&gets(library[count].title)!=NULL&&library[count].title[0]!='\0'){
        puts("Now enter the author.");
        gets(library[count].author);
        puts("Now enter the value.");
        scanf("%f",&library[count++].value);
        while(getchar()!='\n'){
            continue;
        }
        if(count<MAXBKS){
            puts("Enter the next title.");
        }
    }
    if(count>0){
        puts("Here is the list of your books:");
        for(index=0;index<count;index++){
            printf("%s by %s:$%.2f\n",library[index].title,library[index].author,library[index].value);
            fwrite(&library[filecount],size,count-filecount,pbooks);
        }
    }else{
        puts("No books? too bad.\n");
    }
    puts("Bye.\n");
    fclose(pbooks);
    return 0;

}

















本文转sinojelly51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/412492,如需转载请自行联系原作者

相关文章
|
11月前
用人话讲懂memcpy函数的理解和使用
用人话讲懂memcpy函数的理解和使用
用人话讲懂memcpy函数的理解和使用
|
开发者
如何设计一套好的技能buff(二)
如何设计一套好的技能buff(二)
360 0
|
C++
【C++基础】结构struct
【C++基础】结构struct
55 0
|
安全 Devops API
F5是什么意思?从BIG-IP Next核心价值了解F5
F5是什么意思?从BIG-IP Next核心价值了解F5
231 0
F5是什么意思?从BIG-IP Next核心价值了解F5
|
存储 人工智能 编译器
learn_C_deep_5 (温故知新、sigend char a = -128的深度理解、unsigned int类型的写法规范)
learn_C_deep_5 (温故知新、sigend char a = -128的深度理解、unsigned int类型的写法规范)
125 0
|
定位技术 开发者 容器
如何设计一套好的技能buff(一)
如何设计一套好的技能buff(一)
747 0
模拟实现库函数strcpy,对strcpy的进一步理解(深刻理解重叠问题,防止内存与源重叠)
模拟实现库函数strcpy,对strcpy的进一步理解(深刻理解重叠问题,防止内存与源重叠)
109 0
ChIP-seq 分析:文库的复杂性和丰富性(7)
ChIPseq 中的一个潜在噪声源是 ChIPseq 库在 PCR 步骤中的过度放大。这可能会导致大量重复读取,从而混淆峰值调用。
|
存储 C++
C/C++零散知识点汇总之sizeof()和strlen()
C/C++零散知识点汇总之sizeof()和strlen()
|
存储 编译器 C语言
C/C++ 基础之 sizeof 使用(一)
C/C++ 基础之 sizeof 使用
630 0