Problems in C file creation

Asked

Viewed 33 times

0

Good night! I am trying to create a txt file from a client list, for this I created a looping in order to store 2 clients and I hope that the created file stores this data in txt, however it is only saving one client at each run! Can you explain to me what’s going on? I started learning about pointers now, so it’s probably something very simple and although I’m consulting my materials I still can’t find the error.

#include <stdio.h>
#include <locale.h>
 struct lista_clientes
{
    char nome[50];
    int idade;
    int telefone;
    char endereco[100];
};

main()
{
    FILE *arqu_file; /*declaração do ponteiro para criação do arquivo*/
    arqu_file= fopen("Clientes.txt","a+"); /*abertura do arquivo*/
    if (arqu_file == NULL) /*teste do arquivo*/
    {
        printf("Houve um ERRO!");
    }
    int i;
     struct lista_clientes Clientes[2];         
        for (i=0;i<2;i++)       /*laço de repetição a priori para 20 clientes*/
        {
            printf("Nome do cliente: "); scanf(" %s",Clientes[i].nome);
            printf("Idade do cliente: "); scanf("%i",&Clientes[i].idade);
            printf("Telefone do cliente: "); scanf("%i",&Clientes[i].telefone);
            fflush(stdin);          /* necessario para o buffer*/
            printf("Endereco do cliente: "); gets(Clientes[i].endereco);
            fprintf(arqu_file,"%s %i %s %i",Clientes[i].endereco,Clientes[i].idade,Clientes[i].nome,Clientes[i].telefone);
            fclose(arqu_file);
            getc(arqu_file);
        }
}

 

1 answer

0

I believe the problem is the fact that you closed the file inside the is, then when it runs for the second time it is no longer possible to write.

Browser other questions tagged

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