Like Tree2Likes
  • 2 Post By quzah

Linked list scanning from a file?

This is a discussion on Linked list scanning from a file? within the C Programming forums, part of the General Programming Boards category; My task right now is to make a linked list with a friends list. Basically, I have one file that ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Linked list scanning from a file?

    My task right now is to make a linked list with a friends list. Basically, I have one file that is full of 20 users (which I can successfully scan into an array) and another file formatted as such:

    ~~~~~~
    name1
    name2
    name3
    ~~~~~~
    name4
    name5
    etc...

    Where the first name after the divider is the person and the following names are his or her friends.

    I'm sorry if I'm not very clear, but my goal is to scan this file and maked a linked list with each user to his or her friends.

    A lot of my code is commented out because it wasn't working.

    Code:
    typedef struct friend friend_t;
    
    
    typedef struct person
    {
        char name[LEN];
        int age;
        int id_number;
        friend_t* friends;
    }    person_t;
    
    
    struct friend // defines a relationship; carries no data.
    {
        person_t *user;
        struct friend* next;
    };
    
    void *find_friends(person_t *s, friend_t *friend_list, char *separator)
    {
        FILE* friends = fopen("friends.dat", "r");
        char temp[SIZE];
        int i, j;
        
        // for(i=0;i<SIZE;i++)
        fscanf(friends, "%s", temp);
    
    
        /*
        for(i=0;i<SIZE;i++)
            if(temp[i] =! separator)
                friend_list[i].user = temp[i];
                    for(j=i;j<SIZE;j++)
                        if(temp[j] =! separator)
                            friend_list[i].next = temp[j]; */
                            
        friend_t *list = (friend_t*)malloc(sizeof(friend_t));
        list->user = find_user(person_t *s, temp);
    }
    I'm not clear on how to use linked lists. What do?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a program that reads entries from a file one at a time until it reaches the end. Once you can do that, make a program read entries one at a time, and then it copies the one it has just read into a structure. Once you can do that, make those structures be added to a linked list one at a time after you read them.


    Quzah.
    iMalc and dmh2000 like this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Well... this is what I have now, and it's still not working. What am I missing? D:

    It says the friend_t head; variable isn't working.

    This is completely incomplete so ignore everything except the linked list part.

    Code:
    #include <stdio.h>
    #include <strings.h>
    #include <stdlib.h>
    #define SIZE 20
    #define SIZE2 100
    #define LEN 20
    
    
    typedef struct friend friend_t;
    
    
    typedef struct person
    {
        char *name;
        int age;
        int id_number;
        friend_t* friends;
    }    person_t;
    
    
    struct friend
    {
        person_t *user;
        struct friend* next;
    };
    
    
    void print_users(person_t *s)
    {
        int i;
    
    
        for(i=0;i<SIZE;i++)
            printf("\n%s: Age: %d, ID Number: %d\n",s[i].name,s[i].age,s[i].id_number);
    }
    
    
    void find_user(char *tempc, person_t *t, person_t *arr)
    {
        int i;
    
    
        for(i=0;i<SIZE;i++)
        {
            if(strcmp(tempc, arr[i].name) == 0)
                t = arr;
        }
    }
    
    
    friend_t *find_friends(person_t *arr, char *separator, FILE* data)
    {
    
    
        friend_t *t1, *h, *p;
        person_t *t2;
        char *tempc = 0; // used to check for separator
    
    
        fscanf(data, "%s", tempc);
        while(tempc != separator)
         {
            t1 = (friend_t*)malloc(sizeof(friend_t)); // memory allocation
            t2 = (person_t*)malloc(sizeof(person_t)); // "        "
            find_user(tempc, t2, arr); // ?!
            t1->next = NULL;
            t1->user = t2;
            t1->user->name = tempc;
    
    
            if(p == NULL)
                h = t1;
            else
             {
                p->next = t1;
                p = t1;
             }
         }
        return h;
    }
    
    
    int main(void)
    {
        person_t s[SIZE];
        /* Say you array of people is person_t people[20] in your main function.  Then people[0]. friend_list is a head pointer to a struct of friend_t.*/
        friend_t *head = 0;
        char *separator = "~";
        int i;
        int temp = 1;
        int get;
    
    
        FILE* data = fopen("final_data.dat", "r");
    
    
        for(i=0;i<SIZE;i++)
            fscanf(data, "%s %d %d" ,s[i].name,&s[i].age,&s[i].id_number);
    
    
        head = find_friends(s, separator, data);
    
    
        while(temp == 1) {
        printf("List of commands: \n\t(1) View users \n\t(2) Add Friend \n\t(3) Delete Friend \n\t(4) Modify Attribute \n\t(5) Sort by attribute \n\t(6) Find degree of separation \n\t(7) Find user with most friends \n\t(8) Find user with least friends \n\t(9) Check to see if a friendship exists \n\t(10) Quit");
        printf("\n\nEnter a command: ");
        scanf("%d", &get);
    
    
        switch(get)
         {
            case 1:
                print_users(s);
                break;
            case 2:
                // add_friend();
                break;
            case 3:
                // delete_friend();
                break;
            case 4:
                // change_atr();
                break;
            case 5:
                // sort();
                break;
            case 6:
                // separation();
                break;
            case 7:
                // most_fr();
                break;
            case 8:
                // least_fr();
                break;
            case 9:
                // check_fr();
                break;
            case 10:
                temp = 0;
                break;
         }
        }
    
    
        printf("\n\n");
        return 0;
    }
    Last edited by polar_ice; 04-30-2012 at 12:43 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > fscanf(data, "%s %d %d" ,s[i].name,&s[i].age,&s[i].id_number);
    You're missing out the fact that each .name is a POINTER which is presently uninitialised.

    > char *tempc = 0; // used to check for separator
    > fscanf(data, "%s", tempc);
    Well it's initialised at least, but it is a NULL pointer, which is a guaranteed crash on most systems.

    Each time you write a * in your code to create a pointer, you need to stop and think for a while exactly how it's going to be used and where the memory it points to is coming from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list from file
    By kapil1089thekin in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 12:23 PM
  2. Linked List to file
    By RoseGirl in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 03:05 PM
  3. Scanning a list?
    By scarlet00014 in forum C Programming
    Replies: 4
    Last Post: 10-15-2008, 03:25 PM
  4. file into a linked list
    By pilecom in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 12:14 AM
  5. file i/o -linked list
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21