469,097 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

please help solve these linked list and sscanf problems

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

Nov 13 '05 #1
1 2813
On Mon, 18 Aug 2003 11:23:13 GMT, Andrej Hocevar <dr******@volja.net>
wrote:
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>
You need to include <stdlib.h> for malloc(). Also <fcntl.h>
is never used.
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.

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kay | last post: by
6 posts views Thread by Steve Lambert | last post: by
6 posts views Thread by Rylios | last post: by
33 posts views Thread by Martin Jørgensen | last post: by
22 posts views Thread by joshc | last post: by
6 posts views Thread by shapper | last post: by
6 posts views Thread by (2b|!2b)==? | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.