Article View: net.bugs
Article #293scanf returns short count on null field(s)
From: Geoff Collyer
Date: Fri, 07 Sep 1984 01:35
Date: Fri, 07 Sep 1984 01:35
41 lines
1426 bytes
1426 bytes
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
---
Technical Details
Message-ID:
<2069@utcsstat.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utcsstat!geoffShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site utcsstat.UUCP Path: arc1-ca.pugleaf.net!utzoo!utcsstat!geoff From: geoff@utcsstat.UUCP (Geoff Collyer) Newsgroups: net.bugs Subject: scanf returns short count on null field(s) Message-ID: <2069@utcsstat.UUCP> Date: Thu, 6-Sep-84 21:35:12 EDT Article-I.D.: utcsstat.2069 Posted: Thu Sep 6 21:35:12 1984 Date-Received: Thu, 6-Sep-84 21:53:27 EDT Organization: U. of Toronto, Canada Lines: 41