Doubts about how to pick up line values inside a C file randomly

Asked

Viewed 90 times

1

Guys, I’m creating a game called "run the wheel",I have doubt on a part of the code, where I need to pick up words inside a file randomly. My file is this way that I will put down: vegetable 2 lettuce alcega kitchen 2 tablespoon stove automobile 2 brake clutch

When I use fscanf to read the file I can only bring the first of each line. What I needed to do was pick up a random word to print to the user as CLUE. What exactly I’m not getting.

I would like to be able to take every word of every column randomly. My code does not present an error when compiling, but I cannot catch the words from inside the file randomly.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <locale.h>



 typedef struct frase{
    char pista[17];
    int qtd;
    char vetpalavras[3][17];
 }Frase;
main(){
    setlocale(LC_ALL, "Portuguese");

    int i;
        Frase lista;
    FILE *palavra;
    palavra = fopen("PALAVRAS.dat","r");

    if(palavra == NULL){
        printf("Erro na abertura!!");
        return 0;
    }

        fscanf(palavra,"%s %d %s ", lista.pista, &lista.qtd,lista.vetpalavras);
        printf(" A palavra estar associada com: %s  \n",lista.pista);
        printf("Palavra: %s \n ",lista.vetpalavras );

        fclose(palavra);





    system("pause");
}
  • I didn’t understand the layout of your WORDS.DAT file, maybe you copied it from somewhere and didn’t pay attention to the result of the copy.

  • I’m sorry. Vegetable 2 ACELGA LETTUCE Automobile 3 ENGINE EXHAUST CLUTCH Kitchen 3 SAUCEPAN COOKER Reptile 1 JARARACA Mammal 2 MONKEY WHALE

  • I do not know if I understood correctly, but I believe that in reading old words you should read only the amount of existing words, read in the variable Qtd. Make a loop for such a reading. Maybe it’s easier for you to read the entire line with a getline and then use an sscanf to read what’s on the line.

  • Change: fscanf(word,"%s %d %s ", &lane.lane, &lane.Qtd, &lane.vetpalavras); by: fscanf(word,"%s %d %s ",lane.lane, &lane.Qtd); for (i=0; i<Qtd; i++) fscanf(word," %s", lane.vetwords[i]);

  • Try to explain what you mean by "being able to pick up a value from a line at random".

  • For example, imagine that my file has 5 lines. On line 1 has a value 1000 of the integer type. on line 2 a 2000 value of the integer type, that for all lines. type: 100 200 300 400 500 When I run the program 1 time I need it to take a value from one line, when I run again, I need it to take another value from another line. I hope I explained it better. Thank you very much.

  • You will be able to retrieve a different record with the use of the fseek function with the offset being generated from a random number (see Rand function) among the number of existing records. With this approach may occur repetition of previously read lines which is not clear whether it would be an allowed situation or not in your project.

  • Yes repetition is allowed. Guy without wanting to take advantage of you, how would that be with the Rand function? I’m new in the area and I’m a little lost and struggling, still in the first semester of college, but I want to learn.

  • Take an example at: http://www.cplusplus.com/reference/cstdlib/rand/

  • Look, I’m doing it this way, I can’t catch another line. I think that would be the first part. # include <stdio. h> main(){ float grana; FILE *premio; premio = fopen("PREMIOS.DAT","r"); fseek(prize, 0, SEEK_SET); fscanf(prize,"%f", &cash); printf("Prize: %f", cash); }

  • You are doing a single read, maybe you wanted the reading to be in a loop, in addition to the fseek function you fixed the offset as zero, this way will always pick up from the beginning of the file.

  • Ask a new question by posting the definition of your file, at least one example of registry, the struct used and the code of your program. This form of questions and answers is not recommended for Stackoverflow. See the tour [https://answall.com/tour].

  • I’m sorry, but I can’t ask any more questions.

  • Then follow Maniero’s recommendation: edit your question and post all relevant data, your code, what is going wrong and what is the expected result.

  • The community, I’m sorry, is the first time I’ve asked a question. I really need to study Stackoverflow. According to the recommendations of the friends above. I edited my question and hope to be understood and have managed to pass my doubt clearly. Thank you all.

Show 10 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.