#include <stdio.h>
#define MAX_LINE 40
#define FILENAME "myfile.txt"
typedef struct {
int id;
float x_coord;
float y_coord;
char name[MAX_LINE];
} MY_TYPE_T;
MY_TYPE_T object;
int main(){
int i;
FILE * fin;
/* open the inputfile*/
fin =fopen(FILENAME,"r");
if(fin==(FILE*)0)
exit(-1);
/*get the last entry*/
fseek(fin,(sizeof(MY_TYPE_T)*2),SEEK_SET);
fread(&object,(sizeof(MY_TYPE_T)),1,fin);
printf("%d %f %f %s\n",object.id,object.x_coord,object.y_coord,object.name);
/*get the second entry*/
rewind(fin);
fseek(fin,sizeof(MY_TYPE_T),SEEK_SET);
fread(&object,(sizeof(MY_TYPE_T)),1,fin);
printf("%d %f %f %s\n",object.id,object.x_coord,object.y_coord,object.name);
/*get the first entry*/
rewind(fin);
fread(&object,sizeof(MY_TYPE_T),1,fin);
printf("%d %f %f %s\n",object.id,object.x_coord,object.y_coord,object.name);
fclose(fin);
return 0;
}
参考资料:《GUN/LINUX环境编程》M.Tim jones