Enviado em 02/04/2017 - 10:31h
Gente, eu não consigo gravar os dados da minha struct no arquivo binário, alguém pode me ajudar?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct REGISTER{
int TAM_REGISTER;
int tam_name;
char *name;
int tam_email;
char *email;
int tam_number;
int number;
int tam_complement;
char *complement;
}reg;
void arquivaPessoa(){
FILE *fp = fopen("register.dat", "ab");
char nome[50], rua[50], comp[20];
reg *rg;
rg = (reg*) malloc (sizeof(reg));
getchar();
printf("\n>>Nome: ");
scanf("%[^\n]", nome);
rg -> tam_name = strlen(nome);
rg -> name = (char*) malloc((rg -> tam_name + 1) * sizeof(char));
strcpy(rg -> name, nome);
getchar();
printf("\n>>Endereco: ");
scanf("%[^\n]", rua);
rg -> tam_email = strlen(rua);
rg -> email = (char*) malloc((rg -> tam_email + 1) * sizeof(char));
strcpy(rg -> email, nome);
getchar();
printf("\n>>Complemento: ");
scanf("%[^\n]", comp);
rg -> tam_complement = strlen(comp);
rg -> complement = (char*) malloc((rg -> tam_complement + 1) * sizeof(char));
strcpy(rg -> complement, nome);
printf("\n>>Numero: ");
scanf("%d", &rg -> number);
rg -> TAM_REGISTER = ((rg -> tam_name) + (rg -> tam_email) + (rg -> tam_complement) + ( 5 * sizeof(int)));
fwrite(&rg -> TAM_REGISTER, sizeof(int), 1, fp);
fwrite(&rg -> tam_name, sizeof(int), 1, fp);
fwrite(rg -> name, sizeof(rg -> tam_name), 1, fp);
fwrite(&rg -> tam_email, sizeof(int), 1, fp);
fwrite(rg -> email, sizeof(rg -> tam_email), 1, fp);
fwrite(&rg -> tam_number, sizeof(int), 1, fp);
fwrite(&rg -> number, sizeof(int), 1, fp);
fwrite(&rg -> tam_complement, sizeof(int), 1, fp);
fwrite(rg -> complement, sizeof(rg ->tam_complement), 1, fp);
fclose(fp);
free(rg);
}
void recuperaPessoa(){
FILE *fp = fopen("register.dat", "rb");
reg *r;
r = (reg*) malloc(sizeof(reg));
int c = 0;
fseek(fp, 0, SEEK_SET);
while (!feof){
printf("\nRegistro: %d", c);
fread(&r->TAM_REGISTER, sizeof(int), 1, fp);
printf("\nTamanho do registro em bytes: %d", r->TAM_REGISTER);
fread(&r -> tam_name, sizeof(int), 1, fp);
printf("\nTamanho do campo em bytes: %d", r -> tam_name);
r -> name = (char*) malloc(r -> tam_name * sizeof(char));
fread(r -> name, sizeof(r -> tam_name), 1, fp);
printf("\nNome: %s", r -> name);
fread(&r -> tam_email, sizeof(int), 1, fp);
printf("\nTamanho do campo em bytes: %d", r -> tam_email);
r -> email = (char*) malloc(r -> tam_email * sizeof(char));
fread(r -> email, sizeof(r -> tam_email), 1, fp);
printf("\nEndereco: %s", r -> email);
fread(&r -> tam_number, sizeof(int), 1, fp);
printf("\nTamanho do campo em bytes: %d", r -> tam_number);
fread(&r -> number, sizeof(int), 1, fp);
printf("\nNumero: %d", r -> number);
fread(&r -> tam_complement, sizeof(int), 1, fp);
printf("\nTamanho do campo em bytes: %d", r -> tam_complement);
r -> complement = (char*) malloc(r -> tam_complement * sizeof(char));
fread(r -> name, sizeof(r -> tam_complement), 1, fp);
printf("\nComplemento: %s", r -> complement);
free(r);
c++;
}
fclose(fp);
}
int main()
{
int op = -1;
while(op != 0){
printf("\n\n>>1 - Registrar Pessoa");
printf("\n>>2 - Recuperar registros");
printf("\n>>0 - Sair");
printf("\n>> Opcao:");
scanf("%d", &op);
switch(op){
case 1:
arquivaPessoa();
break;
case 2:
recuperaPessoa();
break;
default:
printf("\n>>Opcao invalida, tente novamente<<");
}
}
return 0;
}