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

时间:2015-12-02 15:47:06

标签: c

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


};

2 个答案:

答案 0 :(得分: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[]的数组向用户提供问题。

答案 1 :(得分: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