Skip to content

Instantly share code, notes, and snippets.

@pxpc2
Created May 27, 2019 00:42
Show Gist options
  • Select an option

  • Save pxpc2/957388c86b8358804d0b0c8995f17edf to your computer and use it in GitHub Desktop.

Select an option

Save pxpc2/957388c86b8358804d0b0c8995f17edf to your computer and use it in GitHub Desktop.
dsfsdfdsf
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct registro
{
char nome[101],
telefone[11],
endereco[101],
cep[9],
nascimento[11];
};
struct elemento
{
struct elemento* ant;
struct registro data;
struct elemento* prox;
};
typedef struct elemento* Lista;
void carregarContatos(Lista* lista);
Lista* criarLista();
int insere_inicio(Lista* lista, struct registro r);
int main() {
Lista* lista = criarLista();
carregarContatos(lista);
return 0;
}
/*
* Carrega os contatos do arquivo contatos.txt e os insere em Lista* lista,
* TODO: ordenar depois de inserir
*/
void carregarContatos(Lista* lista)
{
FILE *arq = fopen("contatos.txt", "r");
if (arq == NULL)
{
printf("Erro ao carregar/criar \"contatos.txt\"");
return;
}
char texto[255];
int contador = 0;
while (fgets(texto, 255, arq) != NULL)
{
struct registro r;
if (contador == 5)
{
contador = 0;
insere_inicio(lista, r);
continue;
}
switch (contador)
{
case 0:
strcpy(r.nome, texto);
break;
case 1:
strcpy(r.telefone, texto);
break;
case 2:
strcpy(r.endereco, texto);
break;
case 3:
strcpy(r.cep, texto);
break;
case 4:
strcpy(r.nascimento, texto);
break;
default:
break;
}
contador++;
}
fclose(arq);
}
Lista* criarLista()
{
Lista* lista = (Lista*) malloc(sizeof(Lista));
if (lista != NULL) // se conseguiu alocar
*lista = NULL; // Lista é do tipo elemento*, entao *lista aponta pra um elemento, inicializando como nulo
return lista;
}
int insere_inicio(Lista* lista, struct registro r)
{
if (lista == NULL)
return 0;
struct elemento* no = (struct elemento*) malloc(sizeof(struct elemento));
if (no == NULL)
return 0;
no->data = r;
no->prox = (*lista); // próximo elemento vira o elemento atual que a lista aponta
no->ant = NULL; // nó vai ser o primeiro elemento
if ((*lista) != NULL)
(*lista)->ant = no;
(*lista) = no;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment