title: sscanf rejecting leading zeros in integer reads
body: <p>I want to create a simple "date" read using <code>sscanf</code> which accepts inputs as:</p>
  
  <pre><code>"dd/mm/yyyy"
  </code></pre>
  
  <p>Both "dd" and "mm" fields can be, as much, 2 digits longs (eg. 0, 6 or 11, but not 123). The "years" field can be either 0 or a four digits field. A value of 0 in any of these three fields means the day, month or year of the system have to be taken instead.</p>
  
  <p>That format must be strict, so, if the format of the input doesn't fit the pattern, the user must be notified.</p>
  
  <p>My attempt is:</p>
  
  <pre><code>int d, m, y;
  char const* input = "23/7/1990";
  
  int n = sscanf(input, "%2u/%2u/%4u", &amp;d, &amp;m, &amp;y);
  
  if (n != 3) throw InvalidDate("Invalid format");
  
  // Fill 0 values with system date.
  // Check date correctness with `mktime` and `localtime`.
  </code></pre>
  
  <p>The problem is this <code>sscanf</code> format accepts non-allowed inputs as:</p>
  
  <pre><code>char const* invalid1 = "23/ 12/ 1990";
  char const* invalid2 = "23/12/1990/123whatever......."
  </code></pre>
  
  <p>So, is there any tricks/modifiers to reject leading zeros before integers, to mark the end of string, or to cause a detectable failure if more input is parsed?</p>
  
  <p>For the last case (invalid2; detectable failure at the end of string), a possible solution would be:</p>
  
  <pre><code>int d, m, y;
  char trick;
  
  char const* input = "23/7/1990";
  
  int n = sscanf(input, "%2u/%2u/%4u%c", &amp;d, &amp;m, &amp;y, &amp;trick);
  
  // If it fills four fields, means the input was too long.
  if (fields != 3) throw InvalidDate("Invalid format");
  
  // Fill 0 values with system date.
  </code></pre>
  
  <p>But I don't know if there is a better way to detect the <code>end-of-string</code>. Even more, this "format" (with the leading '%c') causes <code>sscanf</code> believes the input was wrong with valid dates (for example, "23/6/1990" provokes the last char is not fill; if <code>scanf</code> were used instead of <code>sscanf</code>, <code>ferror</code> would be set). I have even tried with <code>"%2u/%2u/%4u\0"</code> but the compiler warns me with "embedded \0 in format".</p>
  
  <p>So, what is the best solution without using regular expressions or <code>stringstream</code>?</p>
  
  <p>By the way, is there other ways to "cheat" <code>sscanf</code>? </p>