Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > please help solve these linked list and sscanf problems

Reply
Thread Tools

please help solve these linked list and sscanf problems

 
 
Andrej Hocevar
Guest
Posts: n/a
 
      08-18-2003
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;
}
}

 
Reply With Quote
 
 
 
 
Nick Austin
Guest
Posts: n/a
 
      08-18-2003
On Mon, 18 Aug 2003 11:23:13 GMT, Andrej Hocevar <(E-Mail Removed)>
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help me solve these two questions Common Man C Programming 28 08-12-2012 11:47 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
LInked list Problems, Please help Rylios@comcast.net C++ 6 12-07-2005 09:53 PM
Can anyone solve these problems?? Kevin Wainhouse Computer Information 3 10-23-2004 02:21 AM



Advertisments