Mail Archives: djgpp/1996/01/24/15:57:39
> From: mambuhl AT ripco DOT com (Martin Ambuhl)
>
> If you change your input string to "abc123 ", you will find the behavior
> is what you expect. Notice that the EOF on "abc123" is encountered
> before the attempt to use "%n", and (7.9.6.2) "If end-of-file is
> encountered during input, conversion is terminated. If end-of-file
> occurs before any characters matching the current directive have been
> read (other than leading white space, where permitted), execution of the
> current directive terminates with an input failure; otherwise, unless
> execution of the current directive is terminated with a matching
> failure, execution of the following directive (if any) is terminated
> with an input failure." The fact that %n does not consume any input is
> irrelevant here.
>
I guess you are quoting Ansi? And how should I get the number of
read characters? sscanf don't return any info about reaching EOF here.
Well, this example is very simple and I can get the number of chars
by looking at strlen(name), but in any more complex real program
it can become a real problem.
>
> >Here is a simple test program:
> >---8<---
> >#include <stdio.h>
>
> >int main()
> >{
> > char name[256];
> > int ch_read;
>
> > printf("sscanf=%d\n",sscanf("abc123","%255[a-zA-Z0-9]%n",name,&ch_read));
> > printf("ch_read=%d\n",ch_read);
> > printf("name=%s\n",name);
>
> > return 0;
> >}
> >---8<---
>
> >It outputs:
> >---8<---
> >sscanf=1
> >ch_read=-1058078715
> >name=abc123
> >---8<---
>
> >That is ch_read is uninitialized and sscanf succseeded! :-(
>
> >I think it is because a bug in doscan.c. It checks 'fileended' variable
> >and immediately exits. But in the format string next token is '%n' which does
> >not require more input.
>
> >This test works correctly on Solaris2.4.
> >---
> >Alexander Lukyanov
> >lav AT video DOT yars DOT free DOT net
>
> --
> * Martin Ambuhl net: mambuhl AT ripco DOT com
> * Chicago, IL (USA)
>
- Raw text -