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 ;
}
}
}