I'm using fread and fwrite but a runtime error occurs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct pessoa{
char nome[20];
char cpf[10];
struct pessoa* next;
}Pessoa;
typedef struct lista{
Pessoa* head;
}lista;
typedef struct hash{
int qtd,table_size;
lista** itens;
}Hash;
void imprimirHash(Hash ha){
int i;
Pessoa* aux;
printf("==========================\n");
for(i=0;i<ha.table_size;i++){
if(ha.itens[i]!=NULL){
aux = ha.itens[i]->head;
while(aux!=NULL){
printf("Nome: %s\n",aux->nome);
printf("Cpf: %s\n",aux->cpf);
aux = aux->next;
}
}
printf("\n");
}
}
void lerPessoa(Pessoa* pes){
printf("Digite o nome: ");
scanf("%s",pes->nome);
printf("Digite o cpf: ");
scanf("%s",pes->cpf);
}
Hash* criaHash(int table_size){
Hash* ha;
ha = (Hash *)malloc(sizeof(Hash));
if(ha){
int i;
ha->table_size = table_size;
ha->itens = (lista**)malloc(table_size * sizeof(lista*));
if(!ha->itens){
free(ha);
return NULL;
}
ha->qtd = 0;
for(i=0;i < ha->table_size; i++){
ha->itens[i] = NULL;
}
}
return ha;
}
void liberaHash(Hash* ha){
if(ha){
int i;
for(i=0; i < ha->table_size; i++){
if(ha->itens[i]){
free(ha->itens[i]);
}
}
free(ha->itens);
free(ha);
}
}
int chaveDivisao(int chave, int table_size){
return(chave & 0x7FFFFFFF) % table_size;
}
int valorString(char *str){
int i, valor = 7;
int tam = strlen(str);
for(i=0; i<tam; i++){
valor = 31 * valor + (int)str[i];
}
return valor;
}
int insereHash(Hash *ha, Pessoa* pes){
if(!ha || ha->qtd == ha->table_size){
return 0;
}
//int chave = al.matricula;
int chave = valorString(pes->nome);
int pos = chaveDivisao(chave, ha->table_size);
printf("\nvalor do hash de %s eh %d e a posicao no vetor eh: %d\n",pes- >nome, chave,pos);
lista** aux;
aux = (lista**)malloc(sizeof(lista*));
//lista *novo;
(*aux) = (lista*)malloc(sizeof(lista));
printf("{add:: %d}",&(*aux));
if(!(*aux)){
return 0;
}
(*aux)->head = pes;
if(ha->itens[pos]!=NULL){
(*aux)->head->next = ha->itens[pos]->head;
ha->itens[pos] = (*aux);
printf("\nproximo\n");
} else {
ha->itens[pos] = (*aux);
(*aux)->head->next = NULL;
printf("\nprimeiro\n");
}
ha->qtd++;
return 1;
}
int buscaHash(Hash* ha, char *str, Pessoa* pes){
Pessoa* aux;
if(!ha){
return 0;
}
int chave = valorString(str);
int pos = chaveDivisao(chave,ha->table_size);
if(ha->itens[pos]!=NULL){
aux = ha->itens[pos]->head;
do{
if(strcmp(str,aux->nome)==0){
*pes = *aux;
return 1;
} else{
aux = aux->next;
}
}while(aux!=NULL);
}
return 0;
}
void deletarItem(Hash* ha, char *str){
Pessoa aux;
if(buscaHash(ha,str,&aux)){
int chave = valorString(str);
int pos = chaveDivisao(chave,ha->table_size);
if(ha->itens[pos]->head->next==NULL){
ha->itens[pos] = NULL;
} else{
ha->itens[pos]->head = ha->itens[pos]->head->next;
}
}
}
main(){
Hash* password;
FILE *f;
Pessoa uendel,aux;
int i;
f = fopen("testes.txt","a+");
password = criaHash(23);
//fread(&password,sizeof(Hash*),1,f);
///*
lerPessoa(&uendel);
insereHash(password,&uendel);
lerPessoa(&aux);
insereHash(password,&aux);
printf("Tamnho == %d\n",password->qtd);
printf("{%d}",buscaHash(password,aux.nome,&aux));
//*/
imprimirHash(*password);
//fwrite(&password,sizeof(Hash*),1,f);
fclose(f);
liberaHash(password);
}