A
Andrej Hocevar
Hello,
below I've attached a test program reading data from a file and
storing that information in a linked list. First problem is that
current->p_name will print only the last item in the file for each
item. Where is the problem? Is it in the sscanf? Whay can't I abandon
tmp_name and just use p_name instead in that sscanf? Second, the
order of the output (with a file of 3 lines) is 1, 2, 0, why? And
why is the item 0 empty? I'm new to linked lists and will be
thankful for clarifications.
I really appreaciate your help, thanks
andrej
--
echo ${girl_name} > /etc/dumpdates
====================================================================
#include <stdio.h>
#include <fcntl.h>
int main ()
{
struct conf
{
char *p_name;
char p_up;
char p_down;
int x;
struct conf *next;
};
FILE *conf_fd;
struct conf *head = NULL;
struct conf *current = NULL;
struct conf *new = NULL;
char *tmp_name, tmp_up, tmp_down;
char buf[BUFSIZ];
int i;
conf_fd = fopen("/tmp/conf", "r");
for (i = 0; fgets(buf, BUFSIZ, conf_fd) != NULL; i++)
{
if (*buf == '\n' || *buf == '#')
{
i--;
continue;
}
sscanf(buf, "%s %c %c ", tmp_name, &tmp_up, &tmp_down);
if (head == NULL)
{
new = (struct conf*) malloc(sizeof(struct conf));
new->next = head;
head = new;
new->p_name = tmp_name;
new->x = i;
}
else
{
current = head;
while (current->next != NULL)
current = current->next;
new = (struct conf*) malloc(sizeof(struct conf));
current->next = new;
new->next = NULL;
current->x = i;
current->p_name = tmp_name;
}
}
current = head;
while (current != NULL)
{
printf("%i %s %c\n", current->x, current->p_name, current->p_up);
current = current->next;
}
}
below I've attached a test program reading data from a file and
storing that information in a linked list. First problem is that
current->p_name will print only the last item in the file for each
item. Where is the problem? Is it in the sscanf? Whay can't I abandon
tmp_name and just use p_name instead in that sscanf? Second, the
order of the output (with a file of 3 lines) is 1, 2, 0, why? And
why is the item 0 empty? I'm new to linked lists and will be
thankful for clarifications.
I really appreaciate your help, thanks
andrej
--
echo ${girl_name} > /etc/dumpdates
====================================================================
#include <stdio.h>
#include <fcntl.h>
int main ()
{
struct conf
{
char *p_name;
char p_up;
char p_down;
int x;
struct conf *next;
};
FILE *conf_fd;
struct conf *head = NULL;
struct conf *current = NULL;
struct conf *new = NULL;
char *tmp_name, tmp_up, tmp_down;
char buf[BUFSIZ];
int i;
conf_fd = fopen("/tmp/conf", "r");
for (i = 0; fgets(buf, BUFSIZ, conf_fd) != NULL; i++)
{
if (*buf == '\n' || *buf == '#')
{
i--;
continue;
}
sscanf(buf, "%s %c %c ", tmp_name, &tmp_up, &tmp_down);
if (head == NULL)
{
new = (struct conf*) malloc(sizeof(struct conf));
new->next = head;
head = new;
new->p_name = tmp_name;
new->x = i;
}
else
{
current = head;
while (current->next != NULL)
current = current->next;
new = (struct conf*) malloc(sizeof(struct conf));
current->next = new;
new->next = NULL;
current->x = i;
current->p_name = tmp_name;
}
}
current = head;
while (current != NULL)
{
printf("%i %s %c\n", current->x, current->p_name, current->p_up);
current = current->next;
}
}