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

From: "bowman" <bowman AT montana DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <39CABE68 DOT D5AA584B AT netzero DOT net> <969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz>
Subject: Re: Extract a value from a string?
Lines: 21
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
Message-ID: <i35z5.4837$8X6.4960@newsfeed.slurp.net>
Date: Sat, 23 Sep 2000 10:49:06 -0600
NNTP-Posting-Host: 208.4.224.152
X-Trace: newsfeed.slurp.net 969727310 208.4.224.152 (Sat, 23 Sep 2000 11:41:50 CDT)
NNTP-Posting-Date: Sat, 23 Sep 2000 11:41:50 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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