Thread: Problems printing a linked list.

  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,490
    *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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 03:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01: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, 09:27 PM