2015-12-02 64 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

相關問題