我正在尝试用 C 语言制作一个问答游戏,我有一个包含问题和答案的文件,格式如下:
爱丁堡是哪个国家的?;苏格兰;爱尔兰;英格兰;
文件中的每一行都有一个问题和三个答案,文件中的第一个答案始终是正确的。 我需要使用这样的结构:
typedef struct{
int answer1;
int answer2;
int answer3;
char*question;
}Questions;
我需要从文件中获取它们,所以我认为我应该创建一个循环,例如:
Questions q;
for(int i = 0; i < fileLength; i++){
fscanf(file, "%s%d%d%d", &q.question, &q.answer1,&q.answer2, &q.answer3);
}
所以我需要循环遍历文件并为每个问题创建一个结构变量并为其提供值,然后将其放入数组中。我的老师告诉我使用多维数组,还有指针。我怎样才能以最好的方式实现这一目标?提前致谢。
最佳答案
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
<小时/>
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
您首先定义您的结构(就像这里,在顶部),
然后您可以像使用变量一样使用这些成员。有点像一个物体
一些资源:http://www.tutorialspoint.com/cprogramming/c_structures.htm
对于一些真实的代码:C, reading from file into structure
关于c - 用 C 语言制作问答游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33781720/