【问题标题】:How to get a random struct from a file?如何从文件中获取随机结构?
【发布时间】:2021-08-08 05:11:19
【问题描述】:

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

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);
}

//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

【问题讨论】:

  • 这能回答你的问题吗? srand() — why call it only once?
  • 如果确定的重复候选人不能解决您的问题,请更新帖子以更好地描述问题,而不是“不起作用”。以minimal reproducible example 形式提供完整代码以及确切的输入、预期结果和实际结果。
  • 有水库搜索。 GIYF
  • @PeterO。我不明白提议的欺骗如何解释所描述的错误

标签: c file random struct


【解决方案1】:

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

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

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

if(randNum==qst.id){
   ^^^^^^^  ^^^^^^
  position  question
  in file      ID

也许你想要:

    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
        }
    }

【讨论】:

    【解决方案2】:

    我刚刚发现出了什么问题。我没有在函数 structCounter() 中的 seek() 之后添加命令 rewind()。现在它似乎工作得很好。谢谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2020-08-30
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多