Thread: Link List output

  1. #1
    Unregistered
    Guest

    Link List output

    what's wrong with this code trying to rad from a file using link list ....

    the output is something like this


    787878789898988
    6
    6456
    56
    56
    56
    56
    56


    something that is not on the txt file.




    void traverse (void);





    /* Structure for printing the id.txt file */

    struct id_card
    {

    int ID;
    struct id_card *nextptr;
    };




    typedef struct id_card node;








    void traverse (void)

    {

    FILE *fp;
    node list1;
    node *newptr, *currptr, *prevptr;


    int NUM, i, ch ,j=0;
    int NUM_2 = 6;


    newptr= (node*)malloc(sizeof(node));

    fp = fopen("id.txt","r");

    if (fp == NULL)
    {
    printf("Unabale to open id.txt file \n");
    exit(1);
    }




    currptr = newptr;
    for ( i = 1; i<=8; i++)
    {
    NUM = getc(fp);
    NUM = NUM-48;

    newptr->ID = NUM;
    printf ("%d",NUM);

    newptr->nextptr=(node*)malloc(sizeof(node));
    newptr=newptr->nextptr;
    }

    if(newptr !=NULL){
    newptr->ID = NUM_2;
    newptr->nextptr=currptr;
    currptr=newptr;
    }


    else
    {
    printf("\n %d not inserted. No memory available.\n",NUM_2);
    }


    do{
    j++;
    printf("\n");
    printf("%d", currptr->ID);
    currptr=currptr->nextptr;
    }
    while(j<=8);


    fclose(fp);

    }




    pls help !!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void traverse(void);
    
    /* Structure for printing the id.txt file */
    struct id_card {
        int ID;
        struct id_card *nextptr;
    };
    
    typedef struct id_card node;
    
    void traverse(void) {
        FILE *fp;
        node *newptr = NULL, *currptr = NULL;
        int NUM, i, j=0;
        int NUM_2 = 6;
    
        newptr= (node*)malloc( sizeof(node) );
    
        fp = fopen( "id.txt","r" );
    
        if(fp == NULL) {
            printf( "Unabale to open id.txt file \n" );
            exit( 1 );
        }
    
        currptr = newptr;
        for(i = 1; i<=8; i++) {
            NUM = getc( fp );
            NUM = NUM-48;
    
            /*!! this is the problem - newptr isn't pointing at a valid node */
            /*!! so trying to dereference it is a bad idea */
            /*!! you've got to malloc a node before trying to store data in it */
            newptr->ID = NUM;
            printf( "%d",NUM );
    
            newptr->nextptr=malloc( sizeof(node) );
            newptr=newptr->nextptr;
        }
    
        if(newptr != NULL) {
            newptr->ID = NUM_2;
            newptr->nextptr=currptr;
            currptr=newptr;
        } else {
            printf( "\n %d not inserted. No memory available.\n",NUM_2 );
        }
    
        do {
            j++;
            printf( "\n" );
            printf( "%d", currptr->ID );
            currptr=currptr->nextptr;
        }
        while(j<=8);
    
        fclose( fp );
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    Do you have any suggestion on how i can do that ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. Weird Output...(Link list)
    By davidvoyage200 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2003, 09:07 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM