I’m trying to read a text file and store each line in a node of a link list of type void*.
Here’s the header file of the list.
#ifndef LINKEDL
#define LINKEDL
struct node_s {
void *data;
struct node_s *next;
};
struct node_s *node_create(void*);
struct node_s *list_insert_after(struct node_s*, void*);
struct node_s *list_insert_beginning(struct node_s*, void*);
int list_remove(struct node_s*, struct node_s*);
int list_foreach(struct node_s*, int(*)(void*));
int printstring(void *s);
#endif
All the linked list functions have been thoroughly tested, so I guess the problem is with how I use it. What I want to achieve is the have one line in each node and what I have now is last line in every node. I guess it has something to do with the char pointers but have already spent two hours on that without a spectacular breakthrough, so maybe someone could help?
Also the list I use is a modified list as seen here.
if (file == NULL)
{
perror("Error opening file");
}
else
{
char mystring[SIZE];
char temp[SIZE];
list = node_create((void*)mystring);
current = list;
while (fgets(mystring, SIZE, file) != NULL)
{
strcpy(temp, mystring);
printf("%d\t%s",counter++,temp);
current=list_insert_after(current, (void*)temp);
}
fclose(file);
}
UPDATE:
Thank you All.
Remove the line:
And change the line:
To