Read each row of a file with different types in c using vector

Asked

Viewed 118 times

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

  • In addition a successful execution of fscanf will return the amount of read items, in case 5 and not 1.

  • 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.

1 answer

0


First you read the name of the "clue", and the amount of words.
Then you read the words, one at a time.

#include <stdio.h> // exit
#include <stdlib.h> // exit

int main(void)
{
  int qtd, i;
  char url[] = "palavras.dat", pista[17], vetpalavras[3][17];

  printf("*\n");

  FILE *arq = fopen(url, "r");
  if (arq == NULL)
  {
    printf("* erro, nao foi possivel abrir o arquivo\n");
    printf("*\n");
    exit(1);
  }

  for (;;)
  {
    // primeiro faz a leitura do nome e da quantidade de palavras
    if (fscanf(arq,"%16s %d", pista, &qtd) != 2)
    {
      // nao conseguiu ler os 2 campos

      // e' fim de arquivo ?
      if (feof(arq))
        printf("* ok, fim de arquivo\n");
      else
        printf("* erro no conteudo do arquivo\n");

      printf("*");
      exit(0);
    }

    // neste ponto leu o nome e a quantidade de palavras

    // agora vai ler as palavras

    if (qtd > 3)
      qtd = 3;

    for (i = 0; i < qtd; i++)
    {
      if (fscanf(arq, "%16s", vetpalavras[i]) != 1)
      {
        printf("* erro no conteudo do arquivo\n");
        printf("*\n");
        exit(2);
      }
    }

    // ok, leu as palavras

    printf("* nome=%-10s qtd=%d", pista, qtd);

    for (i = 0; i < qtd; i++)
      printf(" %s", vetpalavras[i]);

    printf("\n");
  }

  fclose(arq);
}

Testing:

$./390144
*
* nome=Vegetal    qtd=2 ACELGA ALFACE
* nome=Automovel  qtd=3 MOTOR EMBREAGEM ESCAPAMENTO
* nome=Cozinha    qtd=3 PRATO PANELA FOGAO
* nome=Reptil     qtd=1 JARARACA
* nome=Mamifero   qtd=2 BALEIA MACACO
* ok, fim de arquivo
*
$
  • how do I select a line individually? I’m not getting it because it looks like the track variable is overwritten and I can only catch the last line

  • I don’t understand your question

  • I’m doing the wheel-to-wheel game, and I need to draw lots to select one of these tracks and then print it together with the words associated with it. But I can’t do it because I can’t pick it up line by line. For example: the program draws that dat file and selects only one line, dps prints the clue and the words of that line.

  • (1) declare a struct X with the track, Qtd and vetpalavras fields (2) create an array of structs X (3) for each line read, store in the structs array (ps. X is just an illustrative name, choose a suitable name)

  • is giving this error: "request for Member 'Qtd' in Something not a Structure or Union". in which place of the code I place the struct fields to store?

  • open a new question with the changes you made

  • https://answall.com/questions/390167/assignment-to-expression-with-array-type-em-uma-struct-com-matrix

Show 2 more comments

Browser other questions tagged

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