OK here is my updated link list code. It compiles with no errors but it does not run....it freezes.
I get the feeling it has to do with file IO...I am not reading words from the file correctly!!
Any suggestions?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINES 10
#define MAXWORDLENGTH 10
FILE *ifp;
struct word_link
{
char word[MAXWORDLENGTH];
struct word_link *pnext;
};
int main(void)
{
const char ifn[]={"word.txt"};
char line[1+MAXLINES];
struct word_link *phead,*pthis, *pnew;
ifp=fopen(ifn,"r");
if (ifp==NULL)
{
perror(ifn);
exit(1);
}
phead=NULL;
for(;;)
{
fscanf(ifp,"%s", line);
if (feof(stdin)) break;
if ((pnew=(struct word_link *)malloc(sizeof(struct word_link)) )==NULL)
{
fprintf(stderr,"link1: no storage available\n");
exit(1);
}
strcpy(pnew->word,line);
pnew->pnext=phead;
phead=pnew;
}
for(pthis=phead;pthis!=NULL;pthis=pthis->pnext)
printf("%p %s\n",pthis,pthis->word);
while(pnew !=NULL)
{
free(pnew);
}
fclose(ifp);
return 0;
}