mixing scanf and gets

Guy Harris guy at sun.uucp
Mon Apr 7 07:44:35 AEST 1986


> However, it sounds to me like your application would be
> programmed better with the following scheme:
> 
> 	while get-a-line-using-fgets
> 		parse-line-with-sscanf

>From my experience, just about *any* application would be programmed better
using "fgets" and "sscanf" instead of "scanf"....

> By the way, don't use gets(); your input buffer can be
> overrun if an input line is very long, resulting in
> unpredictable malfunctioning of your application.  When
> you use fgets(), it is often wise to test the last
> character in the record and if it is a newline, replace
> it with a 0 string terminator before parsing the record.

And if it is NOT a newline, yell, scream, and bitch that somebody gave you a
line which was too long; otherwise, your program will think that the rest of
the line (i.e., everything after the cutoff) is an additional line.  The
SCCS "delta" command, when reading the output from "bdiff", didn't do this,
and got HORRIBLY confused if the new version of the file had a line longer
than about 510 characters long.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy at sun.arpa	(yes, really)



More information about the Comp.lang.c mailing list