Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!gatech!ncsuvx!mcnc!duke!bet From: bet@orion.mc.duke.edu (Bennett Todd) Newsgroups: comp.lang.c Subject: Re: best way to return (char *) Message-ID: <14944@duke.cs.duke.edu> Date: 12 Jul 89 06:15:31 GMT References: <7800013@gistdev> <2793@solo8.cs.vu.nl> <1989Jun23.124149.1374@twwells.com> Sender: news@duke.cs.duke.edu Reply-To: bet@orion.mc.duke.edu (Bennett Todd) Organization: Diagnostic Physics, Radiology, DUMC Lines: 27 In-reply-to: bill@twwells.com (T. William Wells) As has been said, in general the best mechanism to use depends on other features of the problem at hand. Here's a specific solution that I liked for a particular problem. I wanted to be able to easily loop along line at a time through a file, without having to worry about maximum line lengths. So I wrote a routine getline(3b): char *getline(char *, FILE *); which might be used like this: char *line = NULL; ... while (line = getline(line, fp)) { /* process the line */ } getline(3b) allocates the line buffer if it is passed in NULL for a buffer pointer; on EOF it frees the buffer and returns NULL. It actually malloc's a 2^n byte long buffer, stores n in the first element, and the string starting in the second element, and returns a pointer to the second element. That way it knows the length, and can realloc as necessary to avoid overfilling the buffer. -Bennett bet@orion.mc.duke.edu