Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #43025

question about scanf

From TP <TP@frenoespam.fr.invalid>
Date 2014-04-16 21:06 +0200
Newsgroups comp.lang.c
Subject question about scanf
Message-ID <40832b-tro.ln1@rama.universe> (permalink)
Organization Guest of ProXad - France

Show all headers | View raw


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

Back to comp.lang.c | Previous | NextNext in thread | Find similar | Unroll thread


Thread

question about scanf TP <TP@frenoespam.fr.invalid> - 2014-04-16 21:06 +0200
  Re: question about scanf Kaz Kylheku <kaz@kylheku.com> - 2014-04-16 21:38 +0000
  Re: question about scanf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-16 22:59 +0100
    Re: question about scanf TP <TP@frenoespam.fr.invalid> - 2014-04-21 08:01 +0200
  Re: question about scanf James Kuyper <jameskuyper@verizon.net> - 2014-04-16 18:15 -0400
  Re: question about scanf "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-20 09:47 -0400
  Re: question about scanf viju.kantah@gmail.com - 2014-04-20 07:04 -0700
    Re: question about scanf gazelle@shell.xmission.com (Kenny McCormack) - 2014-04-20 15:24 +0000
    Re: question about scanf "BartC" <bc@freeuk.com> - 2014-04-20 17:10 +0100

csiph-web