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.– anonimo
What would be the functions?
– Mayara Rabelo
The function of reading
fscanf
, but in my opinion it is better to read the whole line withfgets
and treat the string read, for example with the functionstrtok
.– anonimo
It is also possible to read character by character with
getc
and tokenize already in data entry.– user142154
Someone Can Do This By Getting No
– Mayara Rabelo