逐行阅读并存储在一个结构数组中

我正在尝试逐行读取文本文件中的行并将其存储在数组中。 txt文件中有一些问题会被提交给玩家!

这是一些问题!!

1: När kom potatisen till Europa?;A:1300-talet; B:1500-talet; C:900-talet;D:1700-talet\n rätt svar : B 2: I vilken enhet mats elektrisk spänning ?;A:Ampere;B:Volt;C:Joule;D:Watt\n Rätt svar: A 3: Från vilket land har vi fått lego?;A:Tyskland;B:Australien;C:Japan;D:Danmark\n rätt svar : D 

它在瑞典!

我已经创建了一个函数,可以在任何找到分号的地方拆分行! 像这样:

 void readline_andsplit() { char str[500]; char *ptr;// token FILE * fp = fopen("gameee.txt","r"); while(fgets(str, 500, fp)){ // read 500 characters // print what we read for fun ptr = strtok(str, ";"); // split our findings around the " " while(ptr != NULL) // while there's more to the string { printf("%s\n", ptr); // print what we got ptr = strtok(NULL, ";"); // and keep splitting } } fclose(fp); } 

这是主要代码:

 int main() { int i = 0; int numProgs = 0; char* nrofqeustions[50]; char line[80]; int j = 0; char correctanswer; FILE *file; file = fopen("gameee.txt", "r"); while(fgets(line, sizeof line, file)!=NULL) { nrofqeustions[i] = calloc(strlen(line)+1, 1); //add each filename into array of nrofqeustions strcpy(nrofqeustions[i], line); i++; //count number of nrofqeustions in file } //check to be sure going into array correctly //for (j=0 ; j<numProgs+1; j++) { //printf("\n%s", nrofqeustions[j]); //} printf("%s\n",nrofqeustions[0]); fclose(file); return 0; } 

我希望它产生以下输出:

 1: När kom potatisen till Europa? A:1300-talet B:1500-talet C:900-talet D:1700-talet 

当用户选择它时,继续下一个问题! 这是一个测验我对这门语言有点新意!

这是我的结构:

  struct quiz{ char question[x]; char alt [4]; char correctanswer[1]; }; 

要将问题与正确答案一起存储,您需要一个类似于此的结构

 struct quiz { char question[50]; char* alt[4]; char correctanswer[1]; }; 

然后在while循环中,您可以阅读问题并存储在结构中

 struct quiz all_ques[10]; int i = 0; //use i to terminate the loop, as how many questions are there in the file while(fgets(str, 500, fp) || i<4) // read 500 characters { ptr = strtok(str, ";"); // split our findings around the " " strcpy(all_ques[i].questions, ptr); // store the question ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[0] = malloc(10); strcpy(all_ques[i].alt[0], ptr); // store the first option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[1] = malloc(10); strcpy(all_ques[i].alt[1], ptr); // store the second option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[2] = malloc(10); strcpy(all_ques[i].alt[2], ptr); // store the third option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[3] = malloc(10); strcpy(all_ques[i].alt[3], ptr); // store the fourth option fgets(str, 500, fp) strcpy(all_ques[i].correctanswer, str); // store the correct answer i++; } 

在此之后,您可以使用结构数组all_ques[]将问题提供给用户。

 #include #include struct quiz { char questions[50]; char* alt[4]; char correctanswer[1]; }; int main (){ struct quiz all_ques[10]; int i = 0; FILE *haidar; haidar=fopen("gameee.txt","r"); char str[500]; char *ptr; while(fgets(str, 500, haidar)) // read 500 characters { ptr = strtok(str, ";"); // split our findings around the " " strcpy(all_ques[i].questions, ptr); // store the question ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[0] = malloc(10); strcpy(all_ques[i].alt[0], ptr); // store the first option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[1] = malloc(10); strcpy(all_ques[i].alt[1], ptr); // store the second option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[2] = malloc(10); strcpy(all_ques[i].alt[2], ptr); // store the third option ptr = strtok(NULL, ";"); // and keep splitting all_ques[i].alt[3] = malloc(10); strcpy(all_ques[i].alt[3], ptr); // store the fourth option fgets(str, 500, haidar); strcpy(all_ques[i].correctanswer, str); // store the correct answer i++; } } 

对不起,如果我做了一些假的错误!! 真的很新,并试图学习@Haris