[comp.lang.c] Parse file and copy words into doubly linked list?

mathews@ecs.umass.edu (04/16/91)

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;

Yes, this is a programming assignment for a class, and I am not looking
for someone to write it for me, just for suggestions.  I have written
a fcn Parse() which places the words in a dynamically created array
using calloc(), but am having problems adapting it for the structure
above.

Any help would be appreciated.  Thanks in advance.

M.J. Mathews
Univ. of Massachusetts at Amherst
mathews@ecs.umass.edu
mathews@umaecs



	typdef struct

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (04/18/91)

In article <13222.2809ff6e@ecs.umass.edu>, mathews@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.