scanf help again

Discussion in 'C Programming' started by Ramprasad A Padmanabhan, Jul 24, 2003.

  1. Hello all

    I want to read into a string an input from QUERY_STRING how do I
    ensure that scanf reads more chars into the string that it can hold

    eg
    {
    char* s1[10];
    char* data;
    long n;
    data = getenv("QUERY_STRING");

    if(sscanf(data,"s1=%[^&]&offset=%ld",s1,&n) != 2) {
    // ERRROR ..
    }

    ....
    }

    How Do I ensure that the sscanf will reject words bigger than 10

    Thanks
    Ram
     
    Ramprasad A Padmanabhan, Jul 24, 2003
    #1
    1. Advertising

  2. Al Bowers wrote:
    >
    > Ramprasad A Padmanabhan wrote:
    > > Hello all
    > >
    > > I want to read into a string an input from QUERY_STRING how do I
    > > ensure that scanf reads more chars into the string that it can hold
    > >
    > > eg
    > > {
    > > char* s1[10];
    > > char* data;
    > > long n;
    > > data = getenv("QUERY_STRING");
    > >
    > > if(sscanf(data,"s1=%[^&]&offset=%ld",s1,&n) != 2) {
    > > // ERRROR ..
    > > }
    > >

    > Put the field width directive 9 in the scanset conversion
    > specification.
    >
    > %9[^&]
    >


    I have tried this
    If I do this then reading offset will not work
    Ram
     
    Ramprasad A Padmanabhan, Jul 25, 2003
    #2
    1. Advertising

  3. On Fri, 25 Jul 2003 11:13:05 +0530, Ramprasad A Padmanabhan
    <> wrote:

    > Al Bowers wrote:

    <snip>
    > > > I want to read into a string an input from QUERY_STRING how do I
    > > > ensure that scanf reads more chars into the string that it can hold


    ensure scanf does *not* read more, or *prevent* it reading more

    > > >
    > > > eg
    > > > {
    > > > char* s1[10];


    Should be char s1 [10], or char * s1 = whatever e.g. malloc(10).

    > > > char* data;
    > > > long n;
    > > > data = getenv("QUERY_STRING");
    > > >
    > > > if(sscanf(data,"s1=%[^&]&offset=%ld",s1,&n) != 2) {
    > > > // ERRROR ..
    > > > }
    > > >

    > > Put the field width directive 9 in the scanset conversion
    > > specification.
    > >
    > > %9[^&]
    > >

    >
    > I have tried this
    > If I do this then reading offset will not work


    It should work, and does for me.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Aug 4, 2003
    #3
  4. On Mon, 04 Aug 2003 03:23:30 GMT, I wrote:

    > On Fri, 25 Jul 2003 11:13:05 +0530, Ramprasad A Padmanabhan
    > <> wrote:


    [ sscanf'ing e.g. s1=stringvalue&offset=123 limiting s1 size ]

    > > > %9[^&]
    > > >

    > >
    > > I have tried this
    > > If I do this then reading offset will not work

    >
    > It should work, and does for me.
    >

    Och, silly me. You must've meant *if the first (string) value exceeds
    the limit* the &offset= portion does not match, because only part of
    the first value was scanned and "converted". Three approaches,
    assuming you want to silently truncate (ignore) any excess:

    1) Break the string into pieces with strtok, or <nonstandard> strtok_r
    if you need thread-safe or callers (or callees) might use strtok, and
    sscanf each piece; but strtok[_r] modifies the string which is not
    guaranteed allowed for a getenv() value so you should duplicate it:
    char * data = /* nonstd or eq */ strdup (getenv ("QUERY_STRING"));
    char * tok; char s1 [10]; long n;
    if( ! data ) ERROR;
    tok = strtok (data, "&"); if( !tok ) ERROR_QS_EMPTY;
    if( sscanf (tok /* or data */, "s1=%9[^&]", s1) != 1 ) ERROR;
    tok = strtok (NULL, "&"); if( !tok ) ERROR_NO_AMPERSAND;
    if( sscanf (tok, "offset=%ld", &n1) ERROR;
    /* optionally check ! strtok (NULL, "&") */


    2) Find the &'s nondestructively with strchr, and do the string
    handling and (some) parsing yourself:
    char * data = getenv ("QUERY_STRING");
    char * next; char s1 [10]; long n; int fits;
    if( ! data ) ERROR;
    next = strchr (data, '&');
    if( ! next ) ERROR_NO_AMPERSAND;
    if( strncmp (data, "s1=", 3) != 0 ) ERROR;
    fits = min (next-data-3, sizeof s1 -1);
    if( ! fits ) ERROR_S1_EMPTY;
    memcpy (s1, data+3, fits); s1[fits] = '\0';
    if( sscanf (next+1, "offset=%ld", &n) != 1) ERROR;
    or for the last, a tiny bit more efficient:
    if( strncmp (next+1, "offset=", 7) != 0 ) ERROR;
    n = strtol (next+1+7, NULL, 10);

    3) If you really want to use sscanf, you must use two calls to allow
    for the "overflow" conversion failing in the (presumably normal)
    nonoverflow case, but they can be in one statement:
    char * data = getenv ("QUERY_STRING");
    char s1 [10]; long n; int used;
    if( ! data ) ERROR;
    if( sscanf (data, "s1=%9[^&]%*[^&]%n", s1, &used) != 1
    || sscanf (data+used, "&offset=%ld", &n) != 1 ) ERROR;

    If you want to detect overflow and give a specific error, or use
    something other than the truncated value, it's a little more
    complicated; if you don't see how, ask more specifically.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Aug 8, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    408
  2. che
    Replies:
    2
    Views:
    519
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    336
    Maric Michaud
    Jun 9, 2006
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,482
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    699
    Richard Bos
    May 2, 2006
Loading...

Share This Page