Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf help again

Reply
Thread Tools

scanf help again

 
 
Ramprasad A Padmanabhan
Guest
Posts: n/a
 
      07-24-2003
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
 
Reply With Quote
 
 
 
 
Ramprasad A Padmanabhan
Guest
Posts: n/a
 
      07-25-2003
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
 
Reply With Quote
 
 
 
 
Dave Thompson
Guest
Posts: n/a
 
      08-04-2003
On Fri, 25 Jul 2003 11:13:05 +0530, Ramprasad A Padmanabhan
<(E-Mail Removed)> 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 [s]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
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      08-08-2003
On Mon, 04 Aug 2003 03:23:30 GMT, I wrote:

> On Fri, 25 Jul 2003 11:13:05 +0530, Ramprasad A Padmanabhan
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
server not found, try again over and over again- help Bill Penney Firefox 1 07-31-2011 08:10 AM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
HttpSession gets generated again and again!! PLEASE HELP ME!!!! che Java 2 10-10-2005 10:20 PM
help with infinite loops and scanf Rob C Programming 8 07-29-2003 12:28 AM



Advertisments