Parse file and copy words into doubly linked list?

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Thu Apr 18 18:12:51 AEST 1991


In article <13222.2809ff6e at ecs.umass.edu>, mathews at ecs.umass.edu writes:
> Could anyone suggest how to parse a text file and place each word in
> a doubly linked list:
> 	typdef struct list
> 	{
> 	char word[20];
> 	struct list *next;
> 	struct list *prev;
> 	}LIST, *LISTPTR;

I have one comment on this.  Why in the name of sanity is it
char word[20]?  Supposing for argument's sake that the words are to be
English words (so that they will be relatively short),
	echo internationalisation | wc
tells me that "internationalisation" has 21 letters.  A quick scan
through /usr/dict/words revealed 5 more words that would overflow
such a tiny array.  One of the most important things you should do in
your programming assignment is check for such overflows and make sure
that you do something sensible with them.

	while (fscanf(file, "%19s", WordBuffer) == 1) {
	    /* WordBuffer holds the next "word" from the file */
	}

If that doesn't suit your notion of a word, %[ may be of use.
-- 
Bad things happen periodically, and they're going to happen to somebody.
Why not you?					-- John Allen Paulos.



More information about the Comp.lang.c mailing list