文件例题(六)

来源:互联网 发布:3dmax建模算法 编辑:程序博客网 时间:2024/06/05 17:14

【题目】在磁盘文件上存有10个学生的数据。要求将第1、3、5、7、9个学生的数据输入的计算机,并在屏幕上显示出来。

#include<stdio.h>#include<stdlib.h>#define N 10struct student{char name[10];int num;int age;char addr[20];}stu[N];int main(){int i;FILE *fp;    if((fp=fopen("stu1.dat","rb"))==NULL){printf("Cannot open file\n");exit(0);}for(i=0;i<N;i+=2){fseek(fp,i*sizeof(struct student),0);fread(&stu[i],sizeof(struct student),1,fp);printf("%-10s% -5d% -5d%-20s\n",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);}fclose(fp);return 0;}