> k= pNode->nData = getword(word,100,instream);
Do you even get any warnings from this?
It would be better for getword to return a char* (and hence return NULL at the end of input) than what you are doing at the moment.
Code:
#include <stdio.h>
#include <stdlib.h> /* for malloc */
#include <ctype.h>
#define NullChar '\0'
#define NewLine '\n'
struct NODE {
struct NODE *pNext;
struct NODE *pPrev;
char *nData;
} *pHead, *pTail;
void AppendNode(struct NODE *pNode);
void InsertNode(struct NODE *pNode, struct NODE *pAfter);
char *getword(char *word, int lim, FILE * instream);
void AppendNode(struct NODE *pNode)
{
if (pHead == NULL) {
pHead = pNode;
pNode->pPrev = NULL;
} else {
pTail->pNext = pNode;
pNode->pPrev = pTail;
}
pTail = pNode;
pNode->pNext = NULL;
}
void InsertNode(struct NODE *pNode, struct NODE *pAfter)
{
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;
if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;
pAfter->pNext = pNode;
}
int main()
{ /* main returns int - AWLAYS! */
struct NODE *pNode;
FILE *instream = stdin /*fopen("words.csv","r") */ ;
char *word;
char *k = 0;
do {
word = malloc(100 * sizeof *word);
pNode = malloc(sizeof *pNode);
k = pNode->nData = getword(word, 100, instream);
AppendNode(pNode);
} while ( k != NULL );
/*ERROR HERE ::STARTS (only prints one char if used %c else give seg error if used %s*/
for (pNode = pHead; pNode != NULL; pNode = pNode->pNext) {
printf("%s\n", pNode->nData);
}
/*ERROR HERE::ENDS*/
return 0;
}
/* getword: get next word or character from input */
char *getword(char *word, int lim, FILE * instream)
{
int foundword = 0;
char *w = word;
while ( lim > 0 ) {
int c = fgetc(instream);
if ( c == EOF ) { word = NULL; break; }
if ( !foundword && isspace(c) ) continue;
foundword = 1;
if ( isalnum(c) ) {
*w++ = c;
*w = NullChar;
lim--;
} else {
ungetc(c, instream);
break;
}
}
return word;
}
Sort of works, there are a couple of things to fix.