gpt4 book ai didi

c - 如何循环遍历数据文件以填充结构?

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:36 24 4
gpt4 key购买 nike

程序规范:

从以下格式的数据文件中读取问题:

Question

Number of choices

N-amount of choices

Correct answer

例子:

What is the capital of France?

3

Madrid

Sydney

Paris

Paris

一次向用户提出一个问题并跟踪他们的分数等。

我目前拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_LINE_SIZE 60
#define MAX_LIST_SIZE 15
#define MAX_QUIZ_SIZE 10

typedef struct question {
char *question;
char **choices;
int n_choices;
char *correct_answer;
} QUESTION;

typedef struct quiz {
struct question *questions;
int n_questions;
} QUIZ;

char *dupString(const char *s) {
// copies a string

char *dup = malloc(strlen(s) + 1);
strcpy(dup, s);
return dup;
}

void free_choices(QUESTION *q) {
// free memory

for(int i = 0; i < q->n_choices; i++) {
free(q->choices[i]);
}

free(q->choices);
}


int ask(QUESTION *q) {
// Return 1 for correct guess, 0 for incorrect guess.

int choice;

// Print the question
printf("\n%s\n", q->question);

// Print the choices
for (int i = 0; i <= q->n_choices-1; i++) {
printf("%d : %s", i+1, q->choices[i]);
}

// Get user guess
do {
printf("Select an answer [1-%d]: ", q->n_choices);
scanf("%d", &choice);

/* Not sure how to structure here*/
if (strcmp(q->choices[choice-1], q->correct_answer) == 0) {
// if correct return 1
return 1;
}
} while (choice < 1 || choice > q->n_choices);
// Incorrect
return 0;
}


struct question parseQuestion(FILE *pData) {

int qIndex, numChoices;
char question[MAX_LINE_SIZE], temp[MAX_LINE_SIZE], choices[MAX_LINE_SIZE], correctAns[MAX_LINE_SIZE];

QUESTION q = {NULL, NULL, 0, NULL};

// Eat first line = QUESTION
fgets(question, MAX_LINE_SIZE, pData);
q.question = question;

// Eat second line = NUMBER OF CHOICES
fgets(temp, MAX_LINE_SIZE, pData);
numChoices = atoi(temp);
q.n_choices = numChoices;

// Allocate memory
q.choices = calloc(q.n_choices, sizeof(char*));

// Eat nth lines = CHOICES
for (qIndex=0; qIndex<=numChoices-1; qIndex++) {
fgets(choices, MAX_LINE_SIZE, pData);
q.choices[qIndex] = dupString(choices);
}

// Eat nth + 1 line = CORRECT ANSWER
fgets(correctAns, MAX_LINE_SIZE, pData);
q.correct_answer = correctAns;

return q;
}

int main() {

int num = 0; // question being asked
int strikes = 0; // incorrect guesses

FILE* pData;

char *filename = "tickle.txt";
char c;

if ((pData = fopen(filename, "r"))) {

printf("Welcome to the 2014 Quiz-festival!\n\n");
printf("Are you ready to begin? [Y/y]\n");
c = getchar();

if (c == 'Y' || c == 'y') {
QUESTION question = parseQuestion(pData);
ask(&question);
free_choices(&question);
} else {
printf("Come back again.\n");
return 0;
}

} else {
printf("File failed to open.");
}

fclose(pData);
return 0;
}

感谢@alk 如何发现我的错误,已解决。

我仍然无法理解的是如何遍历数据文件并用问题结构填充测验结构。

所以这就是我目前正在努力的地方。据我所知,只要我能让它正常工作,我就非常接近完成这个小程序。

最佳答案

parseQuestion() 复制选项但没有复制问题和答案。

相反,它只是将两个数组的地址复制到本地定义的变量 QUESTION q,该变量在返回时被复制。

问题和答案字符串的内存在函数返回时被释放,之后访问它会调用未定义的行为。

关于c - 如何循环遍历数据文件以填充结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23856482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com