T
TP
Hi everybody,
I want to scan a string for an integer, but I don't want any other character
in the string after the integer, otherwise an error has to be raised.
I have scrutinized the manual page of scanf, the best solution I have found
is something as:
//////////////////
#include <stdio.h>
int main( void )
{
int occurrence;
int nbcharreadjustafteroccurrence;
int nbcharreadatendofthestring;
printf("assignement number=%i\n"
, sscanf( "foo 3bar", "foo %d%n%*s%n"
, &occurrence
, &nbcharreadjustafteroccurrence
, &nbcharreadatendofthestring ) );
printf("characters after assignment=%i\n"
, nbcharreadatendofthestring - nbcharreadjustafteroccurrence );
return 0;
}
//////////////////
$ ./a.out
assignement number=1
characters after assignment=3
$
So I can raise an error if the number of characters is greater than zero.
Is there any better solution?
My idea was at some time to look for "$*[\0]", but of course it does not
work:
http://stackoverflow.com/questions/18024820/difference-between-255-0s-and-255c
Thanks in advance,
TP
I want to scan a string for an integer, but I don't want any other character
in the string after the integer, otherwise an error has to be raised.
I have scrutinized the manual page of scanf, the best solution I have found
is something as:
//////////////////
#include <stdio.h>
int main( void )
{
int occurrence;
int nbcharreadjustafteroccurrence;
int nbcharreadatendofthestring;
printf("assignement number=%i\n"
, sscanf( "foo 3bar", "foo %d%n%*s%n"
, &occurrence
, &nbcharreadjustafteroccurrence
, &nbcharreadatendofthestring ) );
printf("characters after assignment=%i\n"
, nbcharreadatendofthestring - nbcharreadjustafteroccurrence );
return 0;
}
//////////////////
$ ./a.out
assignement number=1
characters after assignment=3
$
So I can raise an error if the number of characters is greater than zero.
Is there any better solution?
My idea was at some time to look for "$*[\0]", but of course it does not
work:
http://stackoverflow.com/questions/18024820/difference-between-255-0s-and-255c
Thanks in advance,
TP