Adding to linked list from external file

This is a discussion on Adding to linked list from external file within the C Programming forums, part of the General Programming Boards category; Hi, I am writing a program at the moment which requires me to scan in data from an external file, ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    Post Adding to linked list from external file

    Hi,

    I am writing a program at the moment which requires me to scan in data from an external file, which to be added to a linked list. The external file is a list of pairs of cities and the distances between them - each piece of data is tab-delimited, and each pair of cities is on a new line. Each node within the linked list should contain the name of city one, the name of city two, and the distance between them. My code so far is shown below:



    Code:
    typedef struct node_pair_info // declare structure for npi database
    {
    char city1[20];
    char city2[20];
    int distance;
    struct node_pair_info *next;
    } NPI;
    
    
    
    NPI * add_nodes(NPI *item){
    int i;
    FILE *cities;
    
    cities = fopen("ukcities.txt", "r");
    if (cities == NULL)
    printf("Unable to open ukcities.txt\n");
    
    else 
    {
    
    NPI *new_node;
    new_node = (NPI *) malloc(sizeof(NPI));
    new_node->next = NULL;
    fscanf(cities, "%s \t %s \t %d\n", new_node->city1, new_node->city2, new_node->distance);
    gotoxy(20,25);
    fprintf(stdout, "%s %s %d\n", new_node->city1, new_node->city2, new_node->distance);
    
    }
    
    fclose(cities);
    
    return 0;
    }
    
    
    
    int main(void)
    {
    NPI *first=NULL, *current=NULL, *last=NULL;
    char response;
    
    system("cls");
    
    gotoxy(25,18); printf("Enter 'a' to add all cars to list");
    gotoxy(40,25); putch(' ');
    gotoxy(25,25);
    printf("Enter command: ");
    response = getch();
    gotoxy(34,40); putch(response);
    
    switch (response)
    {
    case 'a': current = last = add_nodes(last); break;
    }
    
    if (first == NULL) current = first = last;
    
    return 0;
    
    }

    The way which i intend for it to work at the moment is that upon the user entering 'a', all the data will be added to the linked list. The program compiles, but upon running, crashes. Im not certain that the code is correct! If anybody could lend me a hand i would be most grateful!

    Thanks,

    Charlie

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The program compiles, but upon running, crashes. Im not certain that the code is correct!
    Rest assured, that if it compiles, but upon running, crashes, you can be certain that the code is not correct.

    Your indentation is horrible, by the way. Why are you returning 0 when you should be returning a node (according to your function definition)? Also, you are in fact only allocating one node.
    Code:
    NPI * add_nodes(NPI *item){
        int i;
        FILE *cities;
    
        cities = fopen("ukcities.txt", "r");
        if (cities == NULL)
        {
            printf("Unable to open ukcities.txt\n");
        } /* for clarity here */
        else 
        {
            NPI *new_node;
            new_node = malloc(sizeof(NPI));
            /* Don't typecast the return of malloc. */
            if( new_node != NULL )
            {
                new_node->next = NULL;
                fscanf(cities, "%s \t %s \t %d\n", new_node->city1, new_node->city2, new_node->distance);
                gotoxy(20,25);
                fprintf(stdout, "%s %s %d\n", new_node->city1, new_node->city2, new_node->distance);
            }
            fclose(cities); /* only close if it has actually opened */
            return new_node; /* return the new node */
        }
        return NULL;
    }
    Now then, there's much to do here. For starters, what use is item? You pass it to the function, but you don't do anything with it. Also, try checking the return value of your fscanf function to see if you are in fact scanning in the right amount of data.

    Get started with that, and see what you come up with. Post your next code attempt if you get stuck.

    Quzah.
    Last edited by quzah; 03-09-2005 at 01:08 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:58 AM

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