scanf returns short count on null field(s)

Geoff Collyer geoff at utcsstat.UUCP
Fri Sep 7 11:35:12 AEST 1984


At least under format %[, the count scanf returns is too low by the
number of null fields read.  This bug appears in the v7 and 4.2bsd stdio
implementations, so it is likely in all AT&T scanfs, though I haven't checked.

I have added this to the BUGS section of scanf(3S):
---
.PP
The
.I scanf
functions return a count which is too low by the number
of null fields scanned.
---

The follow program demonstrates the bug:
---
#include <stdio.h>

main()
{
	char line[200];
	char s1[200], s2[200], s3[200], s4[200], s5[200];

	while (fgets(line, sizeof line, stdin) != NULL) {
		s1[0] = s2[0] = s3[0] = s4[0] = s5[0] = 0;
		printf("sscanf returned %d\n",
			sscanf(line,
			"%[^:\n]:%[^:\n]:%[^:\n]:%[^:\n]:%[^:\n]\n",
			s1, s2, s3, s4, s5));
		printf("tokens: `%s' `%s' `%s' `%s' `%s'\n",
			s1, s2, s3, s4, s5);
	}
	exit(0);
}
---
Try the following input:
---
a:b:c:d:e
:b:c:d:e
::c:d:e
:::d:e
---



More information about the Net.bugs mailing list