#include "stdio.h" #include "stdlib.h" typedef struct work{ char name[20]; float wage; }work; //typedef 名字重定义 将结构体名字struct work重定义为work int main(){ FILE *fp; char filename[20]; work w; int i; printf("Please inputs filename:\n"); gets(filename); if((fp=fopen(filename,"w"))==NULL) { printf("False open file\n"); exit(0); } printf("Please input the information of 5 employees:\n"); for(i=0;i<5;i++){ scanf("%s%f",w.name,&w.wage); fprintf(fp,"%-10s-%8.2f\n",w.name,w.wage); } printf("Employee information saved successfully in %s file \n",filename); fclose(fp); return 0; }
运行结果
注意:文件名请输入文件绝对路径