如何将写在TXT文件中的英语题目以结构体数组的形式存储并读取?
上个学期学完了c语言,现在在基于c语言做一个课程练习,遇到了问题,对我来说是瓶颈,做不出来,才请教网上的各位大神.希望走过路过的看看,谢谢了!
以下是我存在txt文件中的三个例题:
1.In ___ a job or adancing in one , the ability to read and comprehend quickly can mean the difference between success and failure.
A getting
B applying
C doing
D offering
B
2.You were working too hard. You'd better keep a ___ between work and relaxation.
A promise
B lead
C balance
D diary
C
3.The dog may be a good companion for the old. ___ , the need to take it for walks may be a disadvantage.
A Besides
B However
C Therefore
D Instead
B
(注:题目从开头一直写,直到写完题目后换行,在下一行写选项A,写完选项A后换行至下一行写选项B,写完选项B后换行至下一行写选项C,写完选项C后换行至下一行写选项D,最后换行写正确选项,再空一行书写题目2.等等)
#define N 100
结构体代码如下:
typedef struct test
{
char problem[N];//存放N个问题
char A[N];//存放N个答案A
char B[N];//存放N个答案B
char C[N];//存放N个答案C
char D[N];//存放N个答案D
char answer[N];//存放N个正确答案
}TEST;
(注:problem[i],A[i],B[i],C[i],D[i],answer[i]构成一个题目.比如当i=1时,可以从文件中读取到上述的题目1,并显示在屏幕上.一开始只读取题目和四个选项,待用户答题后再读出正确答案并显示在屏幕上.)
(注:文件操作时,存和读的时候请用fgets或fputs)
(请附代码,谢谢!) c语言 结构体 文件操作 文件存和写 typedef
[解决办法]
文件读取是按指针形式一个字符一个字符扫描的 不能直接从中读取指定位置的结构体 只能是自己一行一行的读 然后用结构体数组把它存放起来
[解决办法]
参考http://www.cnitblog.com/guopingleee/archive/2009/01/18/53850.html
参考http://hi.baidu.com/lulyon/item/7009fde698587e226cabb8a4
1.In ___ a job or adancing in one , the ability to read and comprehend quickly can mean the difference between success and failure.
A getting
B applying
C doing
D offering
B
2.You were working too hard. You'd better keep a ___ between work and relaxation.
A promise
B lead
C balance
D diary
C
3.The dog may be a good companion for the old. ___ , the need to take it for walks may be a disadvantage.
A Besides
B However
C Therefore
D Instead
B
#include <stdio.h>
#include <string.h>
#define QN 200
#define QNQ "200"
#define AN 50
#define ANQ "50"
#define MAXQ 100
typedef struct test {
char problem[QN];
char A [AN];
char B [AN];
char C [AN];
char D [AN];
char answer [AN];
} TEST;
FILE *f;
TEST t[MAXQ];
int i,n,L;
char tmp[QN];
int main() {
f=fopen("test.txt","r");
if (NULL==f) {
printf("Can not open file test.txt!\n");
return 1;
}
i=0;
n=0;
while (1) {
n++;if (NULL==fgets(t[i].problem,QN,f)) break;L=strlen(t[i].problem);if ('\n'==t[i].problem[L-1]) t[i].problem[L-1]=0; else goto QERR;
n++;if (NULL==fgets(t[i].A ,AN,f)) break;L=strlen(t[i].A );if ('\n'==t[i].A [L-1]) t[i].A [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].B ,AN,f)) break;L=strlen(t[i].B );if ('\n'==t[i].B [L-1]) t[i].B [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].C ,AN,f)) break;L=strlen(t[i].C );if ('\n'==t[i].C [L-1]) t[i].C [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].D ,AN,f)) break;L=strlen(t[i].D );if ('\n'==t[i].D [L-1]) t[i].D [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].answer ,AN,f)) break;L=strlen(t[i].answer );if ('\n'==t[i].answer [L-1]) t[i].answer [L-1]=0; else goto AERR;
i++;
if (i>=MAXQ) {
printf("Too many tests!(>%d tests ignored.)",MAXQ);
break;
}
n++;if (NULL==fgets(tmp ,QN,f)) break;
}
fclose(f);
n=i;
for (i=0;i<n;i++) {
printf("tests(%d):\n",i+1);
printf("%s\n",t[i].problem);
printf("%s\n",t[i].A );
printf("%s\n",t[i].B );
printf("%s\n",t[i].C );
printf("%s\n",t[i].D );
printf("%s\n",t[i].answer );
printf("\n");
}
return 0;
QERR:
printf("line %d too long!(>%d characters)\n",n,QN);
return 2;
AERR:
printf("line %d too long!(>%d characters)\n",n,AN);
return 3;
}
#include <stdio.h>
#include <string.h>
#define QN 200
#define AN 50
#define MAXQ 100
typedef struct test {
char problem[QN];
char A [AN];
char B [AN];
char C [AN];
char D [AN];
char answer [AN];
} TEST;
FILE *f;
TEST t[MAXQ];
int i,n,L;
char tmp[QN];
int main() {
f=fopen("test.txt","r");
if (NULL==f) {
printf("Can not open file test.txt!\n");
return 1;
}
i=0;
n=0;
while (1) {
n++;if (NULL==fgets(t[i].problem,QN,f)) break;L=strlen(t[i].problem);if ('\n'==t[i].problem[L-1]) t[i].problem[L-1]=0; else goto QERR;
n++;if (NULL==fgets(t[i].A ,AN,f)) break;L=strlen(t[i].A );if ('\n'==t[i].A [L-1]) t[i].A [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].B ,AN,f)) break;L=strlen(t[i].B );if ('\n'==t[i].B [L-1]) t[i].B [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].C ,AN,f)) break;L=strlen(t[i].C );if ('\n'==t[i].C [L-1]) t[i].C [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].D ,AN,f)) break;L=strlen(t[i].D );if ('\n'==t[i].D [L-1]) t[i].D [L-1]=0; else goto AERR;
n++;if (NULL==fgets(t[i].answer ,AN,f)) break;L=strlen(t[i].answer );if ('\n'==t[i].answer [L-1]) t[i].answer [L-1]=0; else goto AERR;
i++;
if (i>=MAXQ) {
printf("Too many tests!(>%d tests ignored.)",MAXQ);
break;
}
n++;if (NULL==fgets(tmp ,QN,f)) break;
}
fclose(f);
n=i;
for (i=0;i<n;i++) {
printf("tests(%d):\n",i+1);
printf("%s\n",t[i].problem);
printf("%s\n",t[i].A );
printf("%s\n",t[i].B );
printf("%s\n",t[i].C );
printf("%s\n",t[i].D );
printf("%s\n",t[i].answer );
printf("\n");
}
return 0;
QERR:
printf("line %d too long!(>%d characters)\n",n,QN);
return 2;
AERR:
printf("line %d too long!(>%d characters)\n",n,AN);
return 3;
}
#include <stdio.h>
#define N 256
struct test {
char problem[N];
char A [N];
char B [N];
char C [N];
char D [N];
char answer[N];
};
void print_test(struct test *test)
{
printf("%s\n", test->problem);
printf("%s\n", test->A);
printf("%s\n", test->B);
printf("%s\n", test->C);
printf("%s\n", test->D);
printf("%s\n", test->answer);
}
int main()
{
FILE * pFile;
struct test mytest;
char string[N];
pFile = fopen ("mytest.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while (!feof(pFile))
{
if ( fgets (mytest.problem, N, pFile) != NULL && fgets (mytest.A, N, pFile) != NULL &&\
fgets (mytest.B, N, pFile) != NULL && fgets (mytest.C, N, pFile) != NULL &&\
fgets (mytest.D, N, pFile) != NULL && fgets (mytest.answer, N, pFile) != NULL)
print_test(&mytest);
getchar();
fgets(string, N, pFile);//跳过空行,这个只能对两道题之间有一行空行
}
fclose (pFile);
}
return 0;
}