Segmentation fault?

David Caswell dbc at cimage.com
Tue Feb 26 09:03:30 AEST 1991


In article <19099:Feb2505:49:2891 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
.> >  char line[BUFSIZ];		/* line of input */
.  [ ... ]
.> >  if (fgets(line, BUFSIZ, fp) == NULL)
.> Here is another, more subtle bug.  fgets will write up to BUFSIZ characters
.> (in this call) to line.  This is not including the final '\0' character.
.> The best thing to do is declare char line[BUFSIZ + 1], and all will be well.
.
.Uh, no. The fgets() interface was designed so that you could do things
.like fgets(line,sizeof(line),fp). Here it reads at most BUFSIZ - 1
.characters before the terminating null.

That is why it is so funny to see people do
while(fgets(str,1,fp)){
	...
}

for character-at-a-time input.



More information about the Comp.unix.programmer mailing list