Problems printing a linked list.

This is a discussion on Problems printing a linked list. within the C Programming forums, part of the General Programming Boards category; I'm trying to construct a linked list from a *.dat file, and then display the nodes to verify the read-in. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    Unhappy Problems printing a linked list.

    I'm trying to construct a linked list from a *.dat file, and then display the nodes to verify the read-in. But, when I run the program, I only get the last node displayed to the screen.

    Can someone tell me why this is?

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    struct savingsnode
    {
    char comment[32] ;
    char deb_cred[8] ;
    double amt ;
    char date[12] ;
    double bal ;
    struct savingsnode *nextptr ;
    } ;

    typedef struct savingsnode Savings ;
    typedef Savings *saveptr ;

    void save_open( saveptr * ) ;
    void save_display( saveptr * ) ;

    int main()
    {

    saveptr startptr ;

    startptr = malloc( sizeof( Savings ) ) ;

    printf( "\n\nWelcome to Finance Assitant!\n\n" ) ;

    save_open( &startptr ) ;

    save_display( &startptr) ;
    }

    /*****************************************
    Function: save_open
    Purpose: Opens the savings.dat file and
    creates the savings linked list.
    *****************************************/
    void save_open( saveptr *sptr)
    {

    FILE *ptrReadDat ;
    char NewLine[256] = {'\0'} ;
    char *tokenptr ;
    saveptr curptr, newptr ;

    ptrReadDat = fopen( "savings.dat", "r" ) ;
    curptr = *sptr ;
    newptr = NULL ;
    curptr->nextptr = newptr ;

    if ( ptrReadDat == NULL )
    printf( "\nFile could not be opened.\n" ) ;

    else
    {
    while( fgets( NewLine, 256, ptrReadDat ) ) ;
    {
    tokenptr = strtok( NewLine, "\t" ) ;
    strcpy( curptr->comment, tokenptr ) ;
    tokenptr = strtok( NULL, "\t" ) ;
    strcpy( curptr->deb_cred, tokenptr ) ;
    tokenptr = strtok( NULL, "\t" ) ;
    curptr->amt = atof( tokenptr ) ;
    tokenptr = strtok( NULL, "\t" ) ;
    strcpy( curptr->date, tokenptr ) ;
    tokenptr = strtok( NULL, "\n" ) ;
    curptr->bal = atof( tokenptr ) ;
    newptr = malloc( sizeof( Savings ) ) ;
    curptr->nextptr = newptr ;
    curptr = newptr ;
    newptr = NULL ;
    }

    fclose( ptrReadDat ) ;
    }

    }

    /**********************************************
    Function: save_display
    Purpose: Displays the savings account summary.
    **********************************************/
    void save_display( saveptr *sptr )
    {

    saveptr curptr ;
    curptr = *sptr ;

    if ( curptr == NULL )
    printf( "There is no information to display." ) ;

    else
    {
    while ( curptr != NULL )
    {
    printf( "%s\t%s\t%.2f\t%s\t%.2f\n", curptr->comment, curptr->deb_cred,
    curptr->amt, curptr->date, curptr->bal ) ;
    curptr = curptr->nextptr ;
    }
    }

    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    *gasp*
    what, no code tags? to hell i cast you!

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    5
    Well, that was helpful.

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This happened to me once. What happened was I was using malloc and sizeof. What I was doing was taking the size of a pointer and not an actual structure. I did not read all your code, but this might be your problem.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Code:
    void save_open( saveptr *sptr) 
    { 
    
    FILE *ptrReadDat ; 
    char NewLine[256] = {'\0'} ; 
    char *tokenptr ; 
    saveptr newptr, curptr = *sptr; 
    
    ptrReadDat = fopen( "savings.dat", "r" ) ;
    
    if ( ptrReadDat == NULL )
    { 
     printf( "\nFile could not be opened.\n" ) ; 
     return;
    } 
    
    
    
    while( fgets( NewLine, 256, ptrReadDat ) ) 
     { 
      
       newptr = malloc( sizeof( Savings ) ) ; //...allocate new memory...
    
      if(newptr == NULL)
      { 
        printf( "\nCould not allocate resources.\n" ) ; 
        return;
      }
    
       newptr->nextptr = NULL; 
       curptr->nextptr = newptr ; //...point to new memory...
       curptr = curptr->nextptr;  //...advance to new memory for next iteration...
       tokenptr = strtok( NewLine, "\t" ) ; 
       strcpy( newptr->comment, tokenptr ) ; 
       tokenptr = strtok( NULL, "\t" ) ; 
       strcpy( newptr->deb_cred, tokenptr ) ; 
       tokenptr = strtok( NULL, "\t" ) ; 
       newptr->amt = atof( tokenptr ) ; 
       tokenptr = strtok( NULL, "\t" ) ; 
       strcpy( newptr->date, tokenptr ) ; 
       tokenptr = strtok( NULL, "\n" ) ; 
       newptr->bal = atof( tokenptr ) ; 
     } 
    
    fclose( ptrReadDat ) ; 
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM

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