[comp.unix.wizards] scanf quiz...

franco@MIKEY.BBN.COM (Frank A. Lonigro) (07/28/87)

	In a previous unix-wizards article, lopez wrote that he has an
"easy" answer to the SCANF quiz.  And it is just that, EASY.  Although 
his solution works, it has a flaw in that the '$' char cannot appear
in any part of the batch of strings after the '#' sign.  This cuts down
on the randomness of strings and restricts the strings to not have a '$'
in them.

	Here is his EASY solution:

main()
{
   char s1[100], s2[100], s3[100];

   sscanf("one two three four # five six seven", "%s %[^#] \# %[^$]",
      s1, s2, s3);

   printf("s1=%s, s2=%s, s3=%s\n", s1, s2, s3);

} /*main*/

	My solution is similar except I don't use the '$' as a flag
for end of string, plus adds to the randomness of strings and doesn't
restrict them in any way.

main()
{
   char s1[100], s2[100], s3[100];

   sscanf("one two three four # five $six seven", "%s %[^#] \# %[\001-\177]",
      s1, s2, s3);

   printf("s1=%s, s2=%s, s3=%s\n", s1, s2, s3);

} /*main*/

	Let me explain:

                sscanf(msg, "%d %s %[\001-\177]", &x, y, z);
                                       ^^^^^^
	Here what the format string is saying is to use all ASCII
characters but the NULL, which is what you want.

-franco%bbn.com@relay.cs.net