正在加载图片...
你现在所处的位置在:首页语言教学→幻灯片版→第十章文件卖 例从键盘输入4个学生数据,把他们转存到磁盘文件中Qh123c #include <stdio. h>V void display #define size 2 f FILE*fp struct student type Int 1 i char name[10 if(fp=fopen(d: \fengyillexellstu dat", "rb D=NULL Int num i printf(" cannot open filen Int age return char addr[15] 3 stud sIze for(i=0; K<SIZE; 1++ maino i fread(&stud[i], sizeof( struct student type), 1, fp) printf( %o-10s %4d%04d %0-15sIn", stud [i] name, stud[i]. num, stud[]. age, stud[i]. addr) for(i0 i<SIZE; 1++) canf("%s%d%d fclose(fp); display 13你现在所处的位置在:首页→C语言教学→幻灯片版→第十章 文件 13 #include <stdio.h> #define SIZE 2 struct student_type { char name[10]; int num; int age; char addr[15]; }stud[SIZE]; main() { int i; for(i=0;i<SIZE;i++) scanf("%s%d%d%s",stud[i].name,&stud[i].num, &stud[i].age,stud[i].addr); save(); display(); } void save() { FILE *fp; int i; if((fp=fopen("d:\\fengyi\\exe\\stu_dat","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i<SIZE;i++) if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1) printf("file write error\n"); fclose(fp); } void display() { FILE *fp; int i; if((fp=fopen("d:\\fengyi\\exe\\stu_dat","rb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i<SIZE;i++) { fread(&stud[i],sizeof(struct student_type),1,fp); printf("%-10s %4d %4d %-15s\n",stud[i].name, stud[i].num,stud[i].age,stud[i].addr); } fclose(fp); } 例 从键盘输入4个学生数据,把他们转存到磁盘文件中 去
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有