please help solve these linked list and sscanf problems

Discussion in 'C Programming' started by Andrej Hocevar, Aug 18, 2003.

  1. 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;
    }
    }
    Andrej Hocevar, Aug 18, 2003
    #1
    1. Advertising

  2. Andrej Hocevar

    Nick Austin Guest

    On Mon, 18 Aug 2003 11:23:13 GMT, Andrej Hocevar <>
    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.
    Nick Austin, Aug 18, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  2. Replies:
    6
    Views:
    309
    Amedeo Verita
    Dec 7, 2005
  3. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  4. RBCC

    Please Help Linked list problems!

    RBCC, Jan 20, 2011, in forum: C Programming
    Replies:
    0
    Views:
    242
  5. Common Man

    Please Help me solve these two questions

    Common Man, Aug 10, 2012, in forum: C Programming
    Replies:
    28
    Views:
    767
    James Kuyper
    Aug 12, 2012
Loading...

Share This Page