retiring gets(3)

Wonderly gregg at ihlpb.ATT.COM
Fri Nov 11 08:29:30 AEST 1988


>From article <8847 at smoke.BRL.MIL>, by gwyn at smoke.BRL.MIL (Doug Gwyn ):
> In article <1031 at cps3xx.UUCP> usenet at cps3xx.UUCP (Usenet file owner) writes:
>>This may be a naive question, or perhaps I haven't followed the right
>>stories, but what is the problem with using gets versus fgets?
> 
> If you don't know for sure that the input line will fit the buffer
> you've allocated for it, gets() can overrun the buffer (with random
> consequences).  However, if your program can be sure that the line
> will fit, there is nothing wrong with using gets().

I believe that the right thing to do is to use a new function called
nlfgets (str, size, fp), that does exactly as gets(3).  The biggest
concern that most people have about moving from gets to fgets is the
added hassle of doing a 

	if ((t = strchr (buf, '\n')) != NULL)
		*t = 0;

This seems to be a lot of work when you may be processing thousands of
lines of code.  I have written this exact function many times just to
have the benefit of no strchr() call.


-- 
It isn't the DREAM that NASA's missing...  DOMAIN: gregg at ihlpb.att.com
It's a direction!                          UUCP:   att!ihlpb!gregg



More information about the Comp.lang.c mailing list