#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;}
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。


