0
#include <stdio.h>
//inicio do programa
main()
{ //cria estruturas
typedef struct {
char nome[10];
} movimentacoes;
//cria as variavéis
FILE *arq;
movimentacoes nome;
//recebe o nome da conta
int i;
for(i=0;i<5;i++){
printf("Digite o nome:\n ");
gets(nome.nome);
}
//abre o arquivo para gravação
arq = fopen ("nomes.txt", "a");
//grava os dados no arquivo
fprintf(arq,"%s", nome.nome);
//fecha o arquivo
fclose (arq);
}
You’re only storing the last name placed (anyone who wants to edit is welcome,)
Exactly, you are putting the opening, recording and closing of the file out of the loop and therefore will only do so for the last data read. If you want to record 5 files then put the file handling instructions inside the loop.
– anonimo
truth, what a key does not.. kkk
– Daniel Soares
I did here, stored all 5, but the last one came first, it’s not necessarily a mistake,
– Daniel Soares
Are you sure that the file no longer existed previously created on an execution of the version of the program with error? Note that you are using append.
– anonimo
For this case specifically it would be better if you open the file, make a loop reading and writing each name and at the end close the file. You can also open the file with "w" to recreate it every run of the program.
– anonimo
I’ll check if it exists,I tried with w but only read the last name
– Daniel Soares