Good luck, I'm developing a C code that performs data registration on files. However, I have a problem with the registerCustom function, which you will see below between the codes.
Client struct:
typedef struct
{
int idCliente;
char nome[NOME_TAM_MAX];
char CPF[16];
char endereco[ENDERECO_TAM_MAX];
}Cliente;
Macros:
#define NOME_TAM_MAX 51
#define ENDERECO_TAM_MAX 101
Function registerCustomer :
void cadastrarCliente (void)
{
Cliente *newCliente = (Cliente *) malloc(sizeof(Cliente));
if (!newCliente)
{
printf("ERRO DE MEMORIA!!!\n");
exit(-1);
}
else
{
printf("\n--- PRENCHA OS DADOS DE CADASTRO DO CLIENTE ---\n\n");
printf("Digite o ID do cliente: ");
scanf(" %d", &newCliente->idCliente);
printf("Digite o nome do cliente: ");
scanf(" %s", newCliente->nome);
printf("Digite o CPF do cliente: ");
scanf(" %s", newCliente->CPF);
printf("Digite o endereco do cliente: ");
scanf(" %s", newCliente->endereco);
stream = fopen("cliente.txt", "w+b"); // stream é global
if (!stream)
{
fputs("ERRO AO TENTAR LER ARQUIVO!!!\n", stderr);
exit(-1);
}
else
{
fwrite(newCliente, sizeof(Cliente), 1, stream);
fclose(stream);
}
}
return;
}
The error is as follows, after typing the client name and pressing enter, all other attributes are skipped (showing just the contents of the printf) and the function comes to an end. I have inverted the fields, put the CPF first and then address, after reading the CPF, everything is successful, but the error repeats itself when reading the address as well.