-1
Good afternoon, I need to create a C code that works as follows:
Read a file . dat that the first column is the clue, the second the amount of words in that clue, and the following columns are the words. Each line is a different clue, with different words that can have up to 3 positions.
I must create a track variable, int for the quantity and a vector with 3 positions. I tried to do it this way:
#include <stdio.h>
int main(void)
{
char url[]="PALAVRAS.dat", pista[17], vetpalavras[3][17];
int qtd, i;
FILE *arq;
arq = fopen(url, "r");
if(arq == NULL)
printf("Erro, nao foi possivel abrir o arquivo\n");
else
for(i=0;i<5;i++){
if ( (fscanf(arq,"%s %d %s %s %s\n", pista, &qtd, vetpalavras[1],vetpalavras[2],vetpalavras[3]))==1 )
printf("%s %d %s %s %s\n", pista, qtd, vetpalavras[1],vetpalavras[2],vetpalavras[3]);
}
fclose(arq);
return 0;
}
File . dat is as follows:
Vegetal 2 ACELGA ALFACE
Automovel 3 MOTOR EMBREAGEM ESCAPAMENTO
Cozinha 3 PRATO PANELA FOGAO
Reptil 1 JARARACA
Mamifero 2 BALEIA MACACO
My question is: am I using the right function (fscanf) to do this? I’m trying but as each line has a lot of different words it’s not working out. And also for some reason the code ta ignoring the n.
I need to find a way to enumerate each line so I can draw the words.
I’m sorry if it’s a big deal, but I’m really cracking my skull on this. Does anyone have an idea of at least what I can do in any part of this challenge?
You loop i from 0 to 4 but each read overwrites the data from the previous read. It also uses indexes from 1 to 3 for words when it should be from 0 to 2.
– anonimo
In addition a successful execution of fscanf will return the amount of read items, in case 5 and not 1.
– anonimo
but if I use vector like i i don’t know how I could store the 3 different strings being that are 3 different strings for the 5 lines.
– Jackgba