由线读取线和结构的阵列中存储 [英] reading line by line and storing in an array of structure
问题描述
我要看书由行文本文件中一行行并将其存储在数组中。 txt文件中有一些问题将被要求给玩家!
下面的一些问题!
1:NAR柑potatisen到欧洲; A:1300 talet; A:1500 talet; C:900 talet; D:1700 talet \\ n
RATT SVAR:乙2:我vilken enhet垫elektrisk跨越; A:安培; B:伏; C:焦耳; D:瓦\\ n
RATT SVAR:一3:起价vilket土地哈日六FATT LEGO; A:德国这家; B:澳大利亚; C:日本; D:丹麦\\ n
RATT SVAR:D
其在瑞典的!
我已经做了分割线的地方找到一个分号功能!像这样的:
无效readline_andsplit()
{
焦炭海峡[500];
字符* PTR; //令牌
FILE *计划生育=的fopen(gameee.txt,R); 而(与fgets(STR,500,FP)){//读取500个字符
//打印我们的乐趣读
PTR =的strtok(STR,); //分裂围绕我们的研究结果 而(PTR!= NULL)//同时还有更多的字符串
{
的printf(%S \\ n,PTR); //打印我们得到了什么
PTR =的strtok(NULL,); //并保持分裂
} }
FCLOSE(FP);}
和这里的主要code:
INT的main(){
INT I = 0;
INT numProgs = 0;
字符* nrofqeustions [50];
焦线[80];
INT J = 0;
焦炭correctanswer; FILE *文件;
文件= FOPEN(gameee.txt,R); 而(与fgets(行,sizeof的线,档案)!= NULL){
nrofqeustions [I] =释放calloc(strlen的(线)+1,1); //添加每个文件名,nrofqeustions数组
的strcpy(nrofqeustions [I],线);
我++; //文件nrofqeustions的计数
} //检查,以确保进入正确的数组
//对于(J = 0; J&下; numProgs + 1; J ++){
//的printf(\\ n%S,nrofqeustions [J]);
//} 的printf(%S \\ n,nrofqeustions [0]);
FCLOSE(文件); 返回0;
}
我希望它产生以下的输出:
1:NAR柑potatisen到欧洲?
答:1300 talet
A:1500 talet
C:900-talet
D:1700 talet
当用户选择它移动到下一个问题!这是一个测验
我还挺新的我的语言!
下面是我的结构:
结构测验{
字符的问题[X];
炭中高音[4];
炭correctanswer [1];
};
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构测验
{
焦问题[50];
字符* ALT [4];
炭correctanswer [1];
};诠释主(){结构测验all_ques [10];
INT I = 0;FILE *海达尔;
海达尔= FOPEN(gameee.txt,R);
焦炭海峡[500];
字符* PTR;而(与fgets(STR,500,海达尔))//读取500个字符
{
PTR =的strtok(STR,); //分裂围绕我们的研究结果
的strcpy(all_ques [I] .questions,PTR); //存储问题 PTR =的strtok(NULL,); //并保持分裂
all_ques [I] .ALT [0] = malloc的(10);
的strcpy(all_ques [I] .ALT [0],PTR); //将第一个选项 PTR =的strtok(NULL,); //并保持分裂
all_ques [Ⅰ] .ALT [1] = malloc的(10);
的strcpy(all_ques [I] .ALT [1],PTR); //将第二个选项 PTR =的strtok(NULL,); //并保持分裂
all_ques [I] .ALT [2] = malloc的(10);
的strcpy(all_ques [I] .ALT [2],PTR); //存储第三个选项 PTR =的strtok(NULL,); //并保持分裂
all_ques [Ⅰ] .ALT [3] = malloc的(10);
的strcpy(all_ques [Ⅰ] .ALT [3],PTR); //存储第四个选项 与fgets(STR,500,海达尔);
的strcpy(all_ques [I] .correctanswer,STR); //将正确答案 我++;
}
}
抱歉,如果我做了一些假的错误!真正的新和努力学习@Haris
I'm trying to read lines from a text file line by line and store it in an array. the txt file has some question in it which will be asked to the player !
Here's some of the questions !!
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
Its in Sweden !
I have made a function that splits the line wherever it finds a semicolon! like this:
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);
}
And here's the main code:
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;
}
I want it to produce the following output:
1: När kom potatisen till Europa?
A:1300-talet
B:1500-talet
C:900-talet
D:1700-talet
When the user chooses it moves on to the next question ! it's a quiz i'm kinda of new to the language !
Here's my struct :
struct quiz{
char question[x];
char alt [4];
char correctanswer[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++;
}
}
sorry if i did some dummy mistakes!! really new and trying to learn @Haris
这篇关于由线读取线和结构的阵列中存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!