delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/23/15:00:12

Message-ID: <39CCF175.275438D7@netzero.net>
From: "Kenneth A. Strom" <therelic AT netzero DOT net>
X-Mailer: Mozilla 4.75 [en] (Win95; U)
X-Accept-Language: en,pdf
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Extract a value from a string?
References: <39CABE68 DOT D5AA584B AT netzero DOT net> <969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz> <i35z5.4837$8X6 DOT 4960 AT newsfeed DOT slurp DOT net>
X-Original-NNTP-Posting-Host: pppa75-resaleracine1-2r7004.saturn.bbn.com
X-Original-Trace: 23 Sep 2000 14:06:52 EST, pppa75-resaleracine1-2r7004.saturn.bbn.com
Organization: GBM Technologies Ltd
Lines: 34
X-Authenticated-User: therelic
Date: Sat, 23 Sep 2000 18:07:08 GMT
NNTP-Posting-Host: 208.243.164.16
X-Trace: petpeeve.ziplink.net 969732428 208.243.164.16 (Sat, 23 Sep 2000 14:07:08 EDT)
NNTP-Posting-Date: Sat, 23 Sep 2000 14:07:08 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

If the string is...

Capture = 0

Would it be better to strstr for = and then extract from the position
of  = + 1 into another string and then atoi?  Does atoi handle
whitespace?

Or should I search for the first number digit (0-9) and extract from
there to the end and atoi?

Ken.
therelic AT netzero DOT net

bowman wrote:

> Edmund Horner <ejrh AT paradise DOT net DOT nz> wrote in message
> news:969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz...
> > char *str;   // this is the string.
> > int n;
> > n = atoi (str);   // method 1.
> > sscans (str, "%d", &n);   // method 2.
>
> couple of minor points:
> using strtol() instead of atoi allow you to test if the string
> was actually a valid number, as well as allowing a base
> to be specified.
>
> checking the return from sscanf() will verify that a numeric
> string was actually found, converted, and placed in 'n'
>
> otherwise, atoi will happily return 0 on a bogus string and
> sscanf() will leave n as garbage.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019