2 Attachment(s)
List of lists/ Reading from File/ Segmentation Error
Hi!
I have to do a multi list and read words from a file, like this: Attachment 14489
And I'm going nuts with it :(
It's my first time implementing a code like this and I've spent like 10 hours + with the segmentation error...
Before posting the code, 2 things:
InfoSize might have something to do with it. You see, it's a data struct end of term code, so I'm using my professor's code as a kind of base. He always uses InfoSize but never to read characters. I tried changing it a bit but couldn't really do much since I got tons of errors.
Second, the code was in Portuguese, I just translated it so excuse me if the variables aren't that cool in English.
Also, it might seem confusing as to why I did some stuff in a weird way instead of going simple, that is probably because I received the segmentation fault so many times and in so many different places that I had to change it a lot, so it became a little weird.
Struct:
Code:
typedef struct noLDDE
{void *information;
struct noLDDE *next;
struct noLDDE *ant;
struct noLDDE *right;
}NoLDDE,*pNoLDDE;
typedef struct LDDE
{ int InfoSize;
pNoLDDE list;
}LDDE;
Code:
int insertFunction(pLDDE p, void *word, int line_number)
{
pNoLDDE temp, aux, aux2, aux3;
int i = 0;
int ret = FRACASSO;
aux = p->list; //first node
while (i != line_number) //till it finds the corresponding node -on the main list-
{
if (aux == NULL)
{
insertInTheEnd(p, word);
//using this to push another node in the main list
}
if (i == 0){
aux3 = p->list;
aux = aux3->next;
i++;
}
else{
aux3 = aux3->next;
aux = aux3->next;
i++; //all this is just to find the node = to the line I should insert
}
}
if ((temp = (pNoLDDE)malloc(sizeof(NoLDDE))) != NULL) //malloc for the node --secondary list--
{
if ((temp->information = (void *)malloc(p->InfoSize)) != NULL)
{
memcpy(temp->information, word, p->InfoSize);
temp->next = NULL;
if (aux3->right == NULL) //if there is no element in the secondary list
{
aux3->right = temp;
temp->ant = NULL;
}
else
{
aux3 = aux3->right;
while (aux3->next != NULL){ //finding the last one to insert in the end
aux3 = aux3->next;
}
aux3->next = temp;
temp->ant = aux3;
}
ret = SUCESSO;
}
}
else
free(temp);
return ret;
}
/*************** INSERE NO FINAL ***************/
int insertInTheEnd(pLDDE p, void *word)
{ pNoLDDE temp, aux;
int ret = FRACASSO;
if((temp=(pNoLDDE)malloc(sizeof(NoLDDE)))!= NULL)
{
if ((temp->information = (void *)malloc(p->InfoSize)) != NULL)
{
memcpy(temp->information, word, p->InfoSize);
temp->next = NULL;
temp->right = NULL;
if (p->list == NULL)
{
p->list = temp;
temp->ant=NULL;
}
else
{
aux = p->list;
while (aux->next != NULL)
aux = aux->next;
aux->next = temp;
temp->ant=aux;
}
ret = SUCESSO;
}
else
free(temp);
}
return ret;
}
int create(ppLDDE pp, int InfoSize)
{ int ret = FRACASSO;
if(((*pp)=(pLDDE)malloc(sizeof(LDDE)))==NULL)
ret = FRACASSO;
else
{
(*pp)->list = NULL;
(*pp)->InfoSize = InfoSize;
ret = SUCESSO;
}
return ret;
}
void printtext(pLDDE p){
pNoLDDE temp, aux;
if (p->list == NULL){
printf("Empty list!");
return;
}
else{
aux = p->list;
temp = aux->right;
while (aux != NULL){ //loop on the main list, each node is a line
while (temp != NULL){ //loop on the words list
printf("%s ", (char *)temp->information);
temp = temp->next;
}
aux = aux->next;
temp = aux->right;
printf("\n");
}
}
}
int main()
{
pLDDE pList;
FILE *file;
char line[1000];
create(&pList, sizeof(char*));
file = fopen("file.txt", "r");
int line_number = 1;
if (file != NULL){
while (fgets(line, 1000, file) != NULL){ //while it is able to read
char *word = strtok(line, " ");
while (word != NULL){ //until it gets to the end of the line
insertFunction(pList, word, line_number);
word = strtok(NULL, " ");
}
line_number++;
}
}
fclose(file);
printtext(pList);
return 0;
}
And here is the gdb:
Sorry for not making it easy by posting just a part of the code, but I'm really out of options right now, kinda desperate actually.
Thanks~~