

student.data.txt:
Код: Выделить всё
6C 69 00 00 00 00 00 00 30 8A 0E 27 F8 7F 00 00 00 00 00 00 01 00 00 00 13 00 00 00
78 69 65 00 08 00 00 00 00 00 00 00 1A 00 00 00 00 00 00 00 01 00 00 00 12 00 00 00
6C 75 6F 00 00 00 00 00 03 00 00 00 00 00 00 00 1E 16 40 00 01 00 00 00 12 00 00 00
Код: Выделить всё
#ifndef __STUDENT__H__
#define __STUDENT__H__
const int STR_LEN = 20;
typedef struct __student{
char name[STR_LEN];
int gender;
int age;
}Student;
#endif
Код: Выделить всё
#include
#include"student.h"
void read(FILE *fp,int index);
int main(int argc,char const *argv[]){
FILE *fp = fopen("D:/tmp/student.data.txt","r");
if(fp){
fseek(fp,0L, SEEK_END);
long size = ftell(fp);
int number =size/sizeof(Student);
int index = 0;
printf("There is %d data,which do you want to see:",number);
scanf("%d",&index);
read(fp,index-1);
fclose(fp);
}
return 0;
}
void read(FILE *fp,int index){
fseek(fp,index*sizeof(Student), SEEK_SET);
Student stu;
fread(&stu,sizeof(Student),1,fp);
if(fread(&stu,sizeof(Student),1,fp)==1){
printf("The %d student:",index+1);
printf("\t name:%s\n",stu.name);
printf("\t gender:");
switch(stu.gender){
case 0:printf("boy\n");break;
case 1:printf("girl\n");break;
case 2:printf("other\n");break;
}
printf("\t年龄:%d\n",stu.age);
}else{
printf("can't open the document.\n");
}
}
Код: Выделить всё
void read(FILE *fp,int index){
fseek(fp,index*sizeof(Student), SEEK_SET);
Student stu;
fread(&stu,sizeof(Student),1,fp);
if(ferror(fp))
printf("there is an IO trouble\n");
else if(feof(fp))
printf("success to the document\n");
printf("%s\n",stu.name);
printf("%d\n",stu.gender);
printf("%d\n",stu.age);
printf("%d\n",fread(&stu,sizeof(Student),1,fp));
/* if(fread(&stu,sizeof(Student),1,fp)==1){
printf("The %d student:",index+1);
printf("\t name:%s\n",stu.name);
printf("\t gender:");
switch(stu.gender){
case 0:printf("boy\n");break;
case 1:printf("girl\n");break;
case 2:printf("other\n");break;
}
printf("\t age:%d\n",stu.age);
}else{
printf("can't open the document");
}*/
}
И я пытаюсь не использовать «if», прямой вывод «stu.name stu .gender stu.age", правильным ответом будет "luo 0 28", а не "luo 1 18".
Платформа — Windows.
В чем проблема с моим кодом?
Подробнее здесь: https://stackoverflow.com/questions/793 ... trouble-in