user-0 提问: 如何将写在TXT文件中的英语题目以结构体数组的形式存储并读取？

回复:
user: user-0
content: 谢谢七楼，我在看代码，已经初步实现了当初自己所做课程设计所需的功能。

user: user-1
content: #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; }

user: user-2
content: 这样放在文本里面似乎很难确保格式，一些手误就可能造成解析程序不能正确运行。 建议可以将这些文本放入格式化的文件（如xml），然后再用一些成熟的模块来解析文件。 这样相对更容易维护，可扩充性也更强。  P.S.: 似乎这样的文本解析更适合用Python,Perl类的脚本来解析，耗费时间成本更低。 否则用c语言来解析，要把程序调试调试正确，承受得起一些变动，也不是一件简单的事情。 当然，如果楼主以学习的目的来做，那又另作他论了，呵呵。  个人意见，仅供参考。

user: user-3
content: #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; }

user: user-3
content: 没实际编译链接调试，不保证对。仅供参考：   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; }

user: user-4
content: 基础的一些文件操作而已！楼主自己先想想思路，整理下基本的代码思路 自己多动手写写，而且需要用的api也都告诉你了！

user: user-5
content: 参考http://www.cnitblog.com/guopingleee/archive/2009/01/18/53850.html 参考http://hi.baidu.com/lulyon/item/7009fde698587e226cabb8a4  引用 楼主 woyaozhucednCS 的回复:上个学期学完了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） （请附代码，谢谢！）

user: user-6
content: 文件读取是按指针形式一个字符一个字符扫描的 不能直接从中读取指定位置的结构体 只能是自己一行一行的读 然后用结构体数组把它存放起来