please help solve these linked list and sscanf problems

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;
}
}
 
N

Nick Austin

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");

There should be a return code check in case the file does not exist.
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);

You should also check the return code here.

This is also the first signficant error; tmp_name is just a pointer.
It isn't yet initialised to point anywhere. Also if this just
''happens to work'' for the first line of the file it won't for the
second line which will overwrite the string retreived by the first
line.
if (head == NULL)
{
new = (struct conf*) malloc(sizeof(struct conf));

You don't need the cast. You probably added it because the compiler
gave a warning about missing #include <stdlib.h>, but this is the
wrong way to suppress the warning.

Also you should check that new != NULL.
new->next = head;
head = new;
new->p_name = tmp_name;

This assumes that you are going to call malloc() to allocate a
different value for tmp_name each time around the loop.
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;

These two lines are also wrong. current is pointing to the last
item in the list. these should assign new->x and new->p_name.
}
}

current = head;
while (current != NULL)
{
printf("%i %s %c\n", current->x, current->p_name, current->p_up);
current = current->next;
}

return 0;

Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top