• C结构体数组的应用


    #include <stdio.h>
    
    
    //定义结构体存储学生成绩信息
    struct address_list{
        char name[10];
        char adr[20];
        char tel[15];
    } info[100];
    
    void save(char *name, int n){
        FILE *fp;
        int i;
        fp = fopen(name, "wb");
        if(fp == NULL){
            printf("cannot open file %s
    ", name);
            exit(0);
        }
        for(i=0; i<n; i++){
            if(fwrite(&info[i], sizeof(struct address_list),1,fp)!=1){
                printf("file write error
    ");
            }
        }
        fclose(fp);
    }
    
    
    void show(char *name, int n){
        FILE *fp;
        int i;
        fp = fopen(name, "wb");
        if(fp == NULL){
            printf("cannot open file %s
    ", name);
            exit(0);
        }
        for(i=0; i<n; i++){
            fread(&info[i], sizeof(struct address_list),1,fp);
            printf("%15s%20s%20s
    ", info[i].name, info[i].adr, info[i].tel);
        }
        fclose(fp);
    }
    
    int main(){
        int i, n;    
        char filename[50] ;
        printf("how many ?
    ");
        scanf("%d", &n);    //输入学生数
        printf("please input filename:
    ");
        scanf("%s", filename);    //输入文件路径和名称
        printf("please input name, address, telephone:
    ");
        for(i=0; i<n; i++){
            printf("NO%d", i+1);
            scanf("%s%s%s", info[i].name, info[i].adr, info[i].tel);
            save(filename, n);    //调用save 
        } 
        show(filename, n);    //调用show 
        return 0;
    }
  • 相关阅读:
    idea 相关
    dns 相关
    bash 相关
    建造者模式(Builder)
    C#中out 及 ref 区别
    C# 2.0新特性
    Asp.net.Ajax控件学习
    装饰模式(Decorator Pattern)
    职责链模式(Chain of Responsibility Pattern)
    面向对象
  • 原文地址:https://www.cnblogs.com/yuwensong/p/5445982.html
Copyright © 2020-2023  润新知