首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从文件中获取随机结构?

如何从文件中获取随机结构?
EN

Stack Overflow用户
提问于 2021-05-19 07:23:18
回答 2查看 67关注 0票数 0

我有一个包含多个结构的文件,每个结构都包含一个问题及其答案和一个标识号。因此,我创建了一个返回文件中结构数量的函数,以及另一个生成介于1和结构数量之间的随机数的函数。单独使用这两个函数可以很好地工作,但当我尝试在下面的函数中使用生成的随机数时,程序在while的第一个循环中停止...另一方面,当我为randNum定义一个值时,该函数做它应该做的事情:它检查每个结构的id,如果id等于randNum定义的值,则在屏幕上打印问题。

代码语言:javascript
复制
void generalKnowledge(){
    header();
    FILE* arquivo;
    question qst;
    int num=0;
    int temp;

    arquivo= fopen("generalKnowledge.txt", "rb");
    if(arquivo==NULL){
        printf("Falha ao abrir arquivo!\n");//"Failed to open file!"
    }
    else{

        int randNum;
        randNum= generateRandom(structCounter(arquivo));

        while(fread(&qst, sizeof(question), 1, arquivo)==1){
        
            if(randNum==qst.id){
                num++;
                printf(ANSI_COLOR_YELLOW "Questao %d: %s\n\n" ANSI_COLOR_RESET, num, qst.question);
                printf(ANSI_COLOR_MAGENTA "a) %s\n" ANSI_COLOR_RESET, qst.opta);
                printf(ANSI_COLOR_MAGENTA "b) %s\n" ANSI_COLOR_RESET, qst.optb);
                printf(ANSI_COLOR_MAGENTA "c) %s\n" ANSI_COLOR_RESET, qst.optc);
                printf(ANSI_COLOR_MAGENTA "d) %s\n" ANSI_COLOR_RESET, qst.optd);
                printf("\n\n");
            }
        }
    
    }
    fclose(arquivo);
}
代码语言:javascript
复制
//Below, the two first functions that I mentioned.


//This one counts the number of structs in the file
    int structCounter(FILE *arq){
        int count;
        int sz;

        fseek(arq, 0L, SEEK_END);
        sz = ftell(arq);

        count= sz/sizeof(question);

        return count;
    }

//This one generates a random number, using the return of the previous function as a limit
    int generateRandom(int count){
        int random;
        srand(time(NULL));
        random= 1+rand()%count;
        return random;
    }

Here's what happen when I run the code using the random number as a value in randNum

And here's the output when I define a value to randNum and run the code

EN

回答 2

Stack Overflow用户

发布于 2021-05-19 11:57:20

您似乎混淆了问题ID和文件中的位置。

首先,你生成一个随机数来表示文件中的位置,例如,randNum等于3表示第三个问题。

但当你读取文件时,你会将位置与问题ID进行比较:

代码语言:javascript
复制
if(randNum==qst.id){
   ^^^^^^^  ^^^^^^
  position  question
  in file      ID

也许你想要:

代码语言:javascript
复制
    while(fread(&qst, sizeof(question), 1, arquivo)==1){
        num++;             // Increcrement number of questions read, i.e. position
        if(randNum==num){  // Compare randNum to position
            printf(ANSI_COLOR_YELLOW "Questao %d: %s\n\n" ANSI_COLOR_RESET, num, qst.question);
            printf(ANSI_COLOR_MAGENTA "a) %s\n" ANSI_COLOR_RESET, qst.opta);
            printf(ANSI_COLOR_MAGENTA "b) %s\n" ANSI_COLOR_RESET, qst.optb);
            printf(ANSI_COLOR_MAGENTA "c) %s\n" ANSI_COLOR_RESET, qst.optc);
            printf(ANSI_COLOR_MAGENTA "d) %s\n" ANSI_COLOR_RESET, qst.optd);
            printf("\n\n");

            break; // end the loop
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2021-05-19 20:55:06

我刚刚发现哪里出了问题。在函数structCounter()中,我没有在seek()之后添加命令rewind()。现在,它似乎完美地工作了。感谢大家的帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67595112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档