i'm trying to read words from a .txt file and then put them in a linked list and then print them out.
here is the text file:
i have created a list header file (list.h) which includes the information about the list structure and here is my program:Code:hello my birthday is in may hello my birthday is in may
my compiler (dev-c++) doesnt give me any warning or errors, however when i try and run the program it crashes, can anyone see where i have gone wrong? i think the problem is with my reading of files and the in file pointer. any help is appreciated.Code:#include "list.h"
#include <stdlib.h>
/*Function to create a list recursivly*/
LINK create( char h, Element *t )
{
LINK y = calloc( 1, sizeof( Element ) );
y->d = h;
if( t ) t->next = y; /* Only do this if there is a previous element. */
return y;
}
/* Function to read data from file*/
FILE readdata(void)
{
FILE *ifp; /*infile pointer*/
ifp=fopen("birthday","r"); /*Open file birthday for reading only*/
return *ifp;
}
/* Function to insert data into the list*/
LINK insert(FILE *ifp)
{
LINK head = NULL;
LINK tail = NULL;
char h;
fscanf(ifp,"%s",&h);
while(h!=0){
tail = create( h, tail );
fscanf(ifp,"s",&h);
if( !head ) { head = tail; } /* If head == 0 (ie. no list): */
}
printf("\n");
return head;
}
/*Function to print list*/
void print_list(LINK head)
{
/* This 'for' loop executes while head != 0*/
LINK next;
for( ; head ; head = next ) {
printf( "%s", head->d );
next = head->next;
}
}
int main(void)
{
LINK head = NULL;
FILE *ifp;
readdata();
insert(ifp);
print_list(head);
return 0;
}