填空题 以下程序的功能是将文件stud_data中第i个学生的姓名、学号、年龄、性别输出,请把程序补充完整。
#include<stdio.h>
struct student_type
{
char name[10];
int num;
int age;
char sex;
}stud[10];
int main()
{
int i;
FILE 1;
if((fp1=fopen("stud_data", "rb"))==NULL)
{
printf("error!/n");
exit(0);
}
scanf("%d", &i);
fseek( 2);
fread([ 3, sizeof(struct student_type), 1, fp1);
printf("%s%d%d%c/n", stud[i].name, stud[i].num, stud[i].age, stud[i].sex);
fclose(fp1);
return 0;
}
【正确答案】
【答案解析】*fp1
fp1, (i-1)*sizeof(struct student_type), 0
&stud[i] [解析] 该程序算法比较简单,描述为以下几个步骤:(1)打开文件stud_data;(2)输入i;(3)文件指针定位到第i个学生数据的起始位置;(4)读取第i个学生的数据;(5)输出第i个学生的数据。