How to show 3 data at a time in this printf in C language?

Asked

Viewed 51 times

1

I have the following code:

//Leitura de arquivo
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  FILE *pont_arq;
  char texto_str[20];
  int contador; 
  //abrindo o arquivo_frase em modo "somente leitura"
  pont_arq = fopen("dadosCadastrais/arquivo.txt", "r");

  //enquanto não for fim de arquivo o looping será executado
  //e será impresso o texto

  while(fgets(texto_str, 20, pont_arq) != NULL)

  printf("%s", texto_str);



  //fechando o arquivo
  fclose(pont_arq);

  getch();
  return(0);
}

I have the following data in the notepad file(. txt):

paulo,joão,antonio,lais,alguemx,alguem2

In the execution of the program in c is displayed thus:

 paulo,joão,antonio,lais,alguemx,alguem2

How to show 3 words per line? example:

paulo,joão,antonio

lais,alguemx,alguem2
  • The function fgets reads a line, or in your case a maximum of 20 characters. If the words are always separated by the character , then you can read the entire line (increase your limit) and count the commas by inserting a line break every 3 found commas. Another option is to use another read function and stop each comma. Note that your program did 2 readings in while.

  • What would be the functions?

  • The function of reading fscanf, but in my opinion it is better to read the whole line with fgets and treat the string read, for example with the function strtok.

  • It is also possible to read character by character with getc and tokenize already in data entry.

  • Someone Can Do This By Getting No

No answers

Browser other questions tagged

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