#include #include #include #define len sizeof(struct test) struct test { char q[200]; char a1[200]; char a2[200]; char a3[200]; char a4[200]; char ture; struct test *next; }; struct test *head; //全局变量储存第一个结构体的指针 int zts(int m)//将m道题装入内存,输入-1时将返回总题数 { FILE *fp; struct test *p1=NULL; char ch,ch1[5]; int i,n=0,wz,py; if((fp=fopen("E:\\test.txt","r"))==NULL)//只读方式打开文件 { printf("文件打开失败请检查e盘下的test.txt文件。\n"); exit(0); } do { py=1; ch=fgetc(fp); for(i=0;ch!='\n'&&ch!=-1;i++)//循环判断回车符个数 { py++; ch=fgetc(fp); } n++; //储存行数的变量 if(n==m)//如果当前行数与传入参数相等 { if(p1==NULL)//判断是否是第一次开辟内存空间 { p1=(struct test *)malloc(len); head=p1; p1->next=NULL; } else { p1->next=p1; p1=(struct test *)malloc(len); p1->next=NULL; } wz=ftell(fp);//记录指针当前位置 fseek(fp,(wz-py-1),0);//调整指针位置到行首 fscanf(fp,"%s%s%s%s%s%s",&p1->q,&p1->a1,&p1->a2,&p1->a3,&p1->a4,&ch1); fseek(fp,wz+1,0);//调整指针位置到行末 p1->ture=ch1[0]; break;//将数据装入内存后跳出循环 } }while(!feof(fp));//文件结束跳出循环 fclose(fp); return n;//返回题目总数n }