2015-12-02 63 views
-4

我试图从文本文件行逐行读取行并将其存储在数组中。 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]; 


}; 
+1

我不能看到你从哪里调用函数'main()' – Haris

+0

我试过了,但是当我调用它时,它立即打印出所有的问题!我忘了提及即将使用结构是我的所有问题以及答案将被存储! –

+0

ook,所以你想让它打印这个问题,请求用户回答,然后继续下一个问题?这个结构在哪里你想存储你的问题和答案 – Haris

回答

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[]的数组向用户提供问题。

+0

谢谢你@哈里斯的时间!但是当我运行该程序时,我仍然遇到一些错误! –

+0

@HaidarWahid,在你的问题中编辑并添加新的代码。 – Haris

1
#include<stdio.h> 
#include<stdlib.h> 



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

相关问题