Hey guys,
I'm having some problem writing a dynamic structure to a text file. The fact is that I already did the function for reading from the text file to the structure, but I'm trying hard and I can't do the opposite.
So, I have this structure here:
Code:
typedef struct ContaFicheiro {
unsigned int numero;
char nomeUtilizador[20];
char PIN[5];
float saldo;
} ContaFicheiro;
and the array of structures is declared as global variable (because it has to):
Code:
ContaFicheiro *arrayContas;
And well, this is the function I wrote to write the data of the structure in a text file, called "accounts2.txt":
Code:
void escreveAccounts(ContaFicheiro *array, unsigned int ultimaConta) {
FILE* fp;
int i;
int tamanho = sizeof &array / sizeof &array[0];
array = (ContaFicheiro *) malloc(sizeof(ContaFicheiro)*sizeof(array));
if( (fp = fopen("accounts2.txt", "w")) != NULL) {
fprintf(fp, "%u\n", ultimaConta);
for(i = 0; i < tamanho; i++) {
//printf("Numero:%u\tNome:%s\tPIN:%s\tSaldo%f\n", array[i].numero, array[i].nomeUtilizador, array[i].PIN, array[i].saldo);
fprintf(fp, "%u\t%s\t%s\t%f\n", array[i].numero,
array[i].nomeUtilizador,
array[i].PIN,
array[i].saldo);
}
}
else {
perror("\nFicheiro 'accounts.txt' nao existe, impossivel completar operacao com sucesso.");
}
free(array);
fclose(fp);
}
The thing is that it doesn't print any information about the structure. Can you help me with this ?