You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# 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
}