Hi
The following code is supposed to print the words(txt) from a file to stdout. I am using Double Linked list cause I am asked to do so. But the problem is... (marked by ERROR in the code listing), when I run it it gives me a seg fault. instead of using %s if I use %c as mentioned inside, I can only print the 1st char of the words. How can I fix it.. please please
****************************
Code:#include <stdio.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); 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; } void main() { struct NODE *pNode; int i; FILE *instream=fopen("words.csv","r"); char *word[100]; int k=0; while(k != -1) { pNode = malloc (sizeof (struct NODE)); k= pNode->nData = getword(word,100,instream);; AppendNode(pNode); } /*ERROR HERE ::STARTS (only prints one char if used %c else give seg error if used %s*/ for( pNode=pHead;pNode !=Null; pNode=pNode->nNext) { printf("%s",pNode->nData); } /*ERROR HERE::ENDS*/ } /* getword: get next word or character from input */ int getword(char *word, int lim, FILE * instream) { int c; char *w = word; while ((isspace(c = fgetc(instream))) && (c !='\n')) ; if (c != EOF) *w++ = c; if (!isalpha(c) ) { *w = NullChar; return c; } for ( ; --lim > 0; w++) if (!isalnum(*w = fgetc(instream))) { ungetc(*w,instream); break; } *w = NullChar; if (c == EOF) return EOF; return word[0]; }



LinkBack URL
About LinkBacks



It seems like I cant initialize pNode to pHead. why would that be?