Define two structures to represent a line. I need to open a file that contains the values of one line per line, putting the values in a linked list. This I was able to do.
#include<stdio.h>
typedef struct{
int x,y;
}ponto;
typedef struct a{
ponto p1,p2;
struct a *prox;
}no;
no *aloca_no(){
no *a;
if(!(a=(no*)malloc(sizeof(no))))
exit(1);
a->prox = NULL;
return a;
}
void incluir_no(no **lista, no*novo){
if(*lista == NULL){
*lista = novo;
}
else{
no *aux = *lista;
novo->prox = NULL;
while(aux->prox)
aux = aux->prox;
aux->prox = novo;
}
}
void lerDados(no **dadosEntrada, char *local){
FILE *arquivo;
no *acc;
if(!(arquivo = fopen(local,"r")))
exit(1);
else{
printf("Foi\n");
while(!(feof(arquivo))){
acc = aloca_no();
fscanf(arquivo,"%d %d %d %d\n", &(acc->p1.x), &(acc->p1.y), &(acc->p2.x), &(acc->p2.y));
incluir_no(dadosEntrada, acc);
}
fclose(arquivo);
}
}
The lerDados
function works perfectly, it calls incluir_no
to insert at the bottom of the list. For each line of the read file, the lerDados
function creates a node and includes it in the list.
After reading the data, if I run another function to print the read data the code works.
void imprime_lista(no *imprime, char *nome){
no *aux = imprime;
while(aux){
printf("-----------------------%s-----------------------\n%d %d || %d %d\n", nome,aux->p1.x, aux->p1.y, aux->p2.x, aux->p2.y);
aux = aux->prox;
}
}
However, if I try to use another function, which does NOTHING only get 2 pointers as parameter, occore the segmentation failure error ....
void retas_verticais(no *dadosEntrada, no *verticais){
printf("uéé");
}
void main(){
no *dadosEntrada, *verticais, *horizontais, *inclinadas;
char local[50]="/home/lua/Workspace/Algoritmos2/dados.txt";
lerDados(&dadosEntrada, local);
imprime_lista(dadosEntrada, "Dados Brutos");
retas_verticais(dadosEntrada, verticais);
}
The data can be obtained from here -> link